简体   繁体   English

从一个聚合物组件调用另一个函数

[英]Call a function from one polymer component to another

I use Polymer, Html and Javascript. 我使用Polymer,HTML和Javascript。

I want to find any easy way to call a function from one polymer component to another. 我想找到一种从一个聚合物组件到另一个聚合物组件调用函数的简便方法。 Let me describe what I want to achieve. 让我描述一下我想要实现的目标。

I have polymer component myComponent . 我有聚合物成分myComponent In this component container with button called start : 在此组件容器中,带有名为start按钮:

<button on-click="_start" id="start">XyZ</button>

and also second component (let's say it is child component, because it is in myComponent , where I have got function _addNew in <script> section. 还有第二个组件(假设它是子组件,因为它在myComponent ,在<script>部分中有_addNew函数。

Now I want to fire _addNew function by clicking button with #start id. 现在,我想通过单击具有#start id的按钮来触发_addNew函数。 My question is how can I call a function from one polymer component when this function is placed in another? 我的问题是,当一个聚合物组件放置在另一个聚合物组件中时,该如何调用该函数?

I hope you understand me. 我希望你能理解我。

if I understood correctly, basically what you want to do is fire from one component and listen in the other one. 如果我理解正确,那么基本上您想要做的就是从一个组件射击,然后在另一个组件中监听。

So you fire from myComponent, in the _start method like: 因此,您可以通过_start方法中的myComponent进行触发,例如:

this.fire('add:new');

and then to listen to the add:new event in your other component by adding a listener, for this specific event name: 然后针对此特定事件名称,通过添加侦听器来侦听其他组件中的add:new事件:

listeners: {
   'add:new': '_addNew'
},

if it's not explicit enough, please do read the Polymer documentation it's a very nice and well explained documentation. 如果不够明确,请阅读Polymer文档,它是一个非常不错且说明充分的文档。

Hope it helps. 希望能帮助到你。

In my case this is not working, because this.fire is going "up", to the parent component, not to a child component. 就我而言,这是行不通的,因为this.fire正在“向上”运行到父组件,而不是子组件。

In my case this one help: 就我而言,这有帮助:

this.$$('component-name').function-name();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 聚合物JS将功能从一个组件传递到另一个组件? - Polymer JS passing function from one component to another? 如何使用按钮从一个组件调用函数到另一个组件 - How to call function from one component to another component with Button 如何使用Polymer 1.0上的观察器通过onclick函数将日期从一个组件发送到另一组件? (不使用本地存储) - How to send date from one component to another component by onclick function using observers on Polymer 1.0? (without using local-storage) 如何将 function 从一个组件调用到另一个组件? 反应 - How can I call a function from one component to another? React 从另一个组件调用函数 - Call function from another component 是否可以将输入的函数传递给Polymer中的另一个组件 - Is it possible to pass a function on input to another component in Polymer 如何通过在另一个组件中单击按钮在一个 React 组件中调用 function - How to call a function in one React component by button click in another component 如何从另一个组件调用function到另一个组件 - How to call function from another component to another component React Js 从另一个组件调用 Function - React Js Call Function from another component 从另一个组件调用函数(React) - Call function from another component (React)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM