【发布时间】:2020-03-28 19:45:57
【问题描述】:
在某个时间点,父自定义元素可以访问其子元素,然后才能使用自定义方法。
class CustomParent extends HTMLElement {
connectedCallback() {
// works
this.children[0].textContent = "bar";
// works
setTimeout(() => this.children[0].test(), 0);
// throws a Type error
this.children[0].test();
}
}
customElements.define("custom-parent", CustomParent);
class CustomChild extends HTMLElement {
test() {
this.textContent = "baz";
}
}
customElements.define("custom-child", CustomChild);
document.body.innerHTML = `
<custom-parent>
<custom-child>foo</custom-child>
</custom-parent>
`;
这怎么可能?推迟this.children[0].test()是否安全?
【问题讨论】: