[英]Refer to sibling Polymer element in Dart?
For the past few hours I have been struggling to refer to a sibling element within my Polymer project. 在过去的几个小时中,我一直在努力在我的Polymer项目中引用同级元素。 Imagine the following setup:
想象以下设置:
/* main.html */
<link rel="import" href="siblingA.html">
<link rel="import" href="siblingB.html">
<polymer-element name="app-main">
<template>
<app-siblingA></app-siblingA>
<app-siblingB></app-siblingB>
</template>
<script type="application/dart" src="main.dart"></script>
</polymer-element>
/* siblingA.html, nothing special about it */
<polymer-element name="app-siblingA">
<template>
<button on-click="{{doSomething))">Do something</button>
</template>
<script type="application/dart" src="siblingA.dart"></script>
</polymer-element>
/* siblingA.dart */
import 'package:polymer/polymer.dart';
import 'dart:html';
@CustomTag('app-siblingA')
class SiblingA extends PolymerElement {
bool get applyAuthorStyles => true;
SiblingA.created() : super.created() {
}
void doSomething(MouseEvent e, var detail, Node target) {
var profile = document.querySelector('app-siblingB');
print(profile); // This is always null, why?
}
}
Now I can get my app-main
node from the document, but it fails on getting sibling elements. 现在,我可以从文档中获取我的
app-main
节点,但是在获取同级元素时失败。 I have tried getting the sibling element via the shadowRoot
without success. 我试图通过
shadowRoot
获得同级元素, shadowRoot
没有成功。
How can I get a sibling element, in this case app-siblingB
, from the document
or shadowRoot
? 如何从
document
或shadowRoot
获取同级元素,在本例中为app-siblingB
shadowRoot
?
Your siblings are within the shadowDOM of <app-main>
. 您的兄弟姐妹在
<app-main>
的shadowDOM中。 document.querySelector()
doesn't reach into the shadowDOM of an element. document.querySelector()
不会进入元素的shadowDOM。
This should work 这应该工作
(parentNode as ShadowRoot).querySelector('app-siblingB');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.