Query elements inside of a custom element

后端 未结 1 699
栀梦
栀梦 2020-12-18 13:46

I have two custom elements. The one of them is nested to another one. Something like this:



        
相关标签:
1条回答
  • 2020-12-18 14:00
    @override
    void attached() { 
      // attached is an example the code an be placed somewhere else
      // but some places are executed before the childs are added 
    
      super.attached();
    
      var nodes = (shadowRoot.querySelector('content') as ContentElement).getDistributedNodes();
    
      var myElement = nodes.firstWhere((e) => e is MyDecorator); // not sure about this `e is MyDecorator`
      // you have to choose some way to identify the node in the result
      // I currently have no example where I can try it myself
    }
    

    if the tag has an id attribute like id='c1' then this works too

      var nodes = ($['c1'] as ContentElement).getDistributedNodes();
    
    0 讨论(0)
提交回复
热议问题