Firing an event from parent to child element in Polymer 2.0

百般思念 提交于 2019-12-11 08:03:53

问题


Can somebody provide an example of sending an event from parent to child in Polymer 2.o custom element?

I tried with following:

<child-element> 
this.addEventListener('dbinit', this._evdbInitStatus);

and

<parent-element>
             this.dispatchEvent(new CustomEvent('dbinit', {detail: {kicked: true}}));

The call back does not get invoked.


回答1:


This is not really related to Polymer itself, as you can see in the documentation on how events work, here, events capturing stops at the element that triggered the event and the event bubbling (as the name suggests, the events bubble up) starts from the element and goes up in the tree.

So, in other words, an event fired by a parent element won't be captured by a child element. You will need to use a data binding to pass data down.

To keep a consistent, predictable flow of data, in general, it's better anyway if data travels down via data binding and up via events.



来源:https://stackoverflow.com/questions/50286986/firing-an-event-from-parent-to-child-element-in-polymer-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!