[英]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.