How to get a callback when a custom element *and it's children* have been initialized

北城余情 提交于 2019-11-30 20:25:12

Actually, there are many different ways to achieve this with nested custom elements.

The direct way is to add a custom callback to the parent element that will be called by the child element from its attachedCallback method:

ParentElement.childAttachedCallback = function ()
{
    console.log( this.childNodes[0].getName() )
}

ChildElement.attachedCallback = function()
{
    this.parentElement.childAttachedCallback()
}

In a complex scenario you could use instead:

  • a Promise object set by the parent and resolve by one or more childs,
  • Custom Events fired by childs and caught by the parent,
  • a MutationObserver object set to observe changes in the parent's child list...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!