我想将对在标记中创建的WebComponent实例的引用传递给WebComponent dart类。例如:
的HTML:
<element name="x-container>
<template iterate="x in xs">
<x-content-elem>
<x-item item="{{x}}" top-container="{{lexical-scoped-ref-to-container}}">
</x-content-elem>
</template>
...
我正在寻找一种将x容器引用到x-item.top-container属性的方法。最主要的是x-item可能以某种复杂的方式嵌套,因此进行动态查找可能很困难或不够鲁棒。
最佳答案
您可以使用DOM查找父元素。这样的事情应该起作用。
在x-item内部:
Container container = this.parent.xtag;
自定义元素的作用类似于页面上的节点。
elem.xtag
getter返回Dart对象,该对象支持页面上的节点。如果它们之间还有其他元素,您仍然可以使用CSS查询来查找所需的元素。
关于dart - 如何引用WebComponent实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369597/