【发布时间】:2017-11-28 19:14:25
【问题描述】:
我有一个基本的 CustomElement,但我遇到了以下问题:
<template id="custom-element">
<h1>Example 1</h1>
</template>
<script>
class CustomElement extends HTMLElement {
constructor() {
super(); // always call super() first in the ctor.
let shadowRoot = this.attachShadow({mode: 'open'});
const template = document.querySelector('#custom-element');
const instance = template.content.cloneNode(true);
shadowRoot.appendChild(instance);
}
connectedCallback() {
console.log("Connected");
}
disconnectedCallback() {
}
attributeChangedCallback(attrName, oldVal, newVal) {
}
}
window.customElements.define('custom-element', CustomElement);
</script>
我在控制台中收到此错误:
未捕获的类型错误:无法读取 null 的属性“内容”
这是因为const template总是null。这以前有效,但我不知道是否有任何改变,现在它不起作用。我正在使用 Chrome 版本 62.0.3202.94(官方版本)(64 位)
请问这方面有什么帮助吗?
【问题讨论】:
标签: javascript web-component shadow-dom custom-element