Polymer 2.0 access a parent component from child component

主宰稳场 提交于 2019-12-02 13:25:27

Calling a parent function from child ;

child-component.html (polymer 2.x)

this.dispatchEvent(new CustomEvent('upway-func', {detail: {op:"Optionally I can send some data"}}));

child-component.html (polymer 1.x)

 this.fire('upway-func', {detail: {op:"Optionally I can send some data"}});

parent-component.html

...
<child-component on-upway-func='_someFunction'></child-component>

...
_someFunction(d){
  console.log(d.detail.op); // "Optionally I can send some data"
 }

Here this link for more detail

Have you tried using this.parentElement?

Ask yourself: if I were a span in a div, how would I get to the div?

If you want to find a particular element in the ancestor chain by selector, you can use .closest() in good browsers or https://github.com/jonathantneal/closest

The message system mentioned in another answer can work but doesn't help you with positional relationship traversing which I assume your question was requiring. Passing ids and/or objects down can be used with messages in some cases.

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