【发布时间】:2017-04-04 19:30:18
【问题描述】:
我有一个带有以下构造函数的 CustomElement:
export default class SomeCustomElement extends HTMLElement {
constructor(templateId) {
super();
this.insertTemplateInstance(templateId);
}
...
}
我可以毫无问题地在 Chrome 中注册该元素。
但是使用 Firefox 和 webcomponents-loader.js 从 https://github.com/webcomponents/webcomponentsjs 加载的 polyfill 我在调用 super() 时收到错误消息 TypeError: Illegal constructor。
有人知道这是什么原因吗?
更多背景:
自定义元素的注册过程如下:
window.addEventListener("WebComponentsReady", function () {
customElements.define(elementName, SomeCustomElement);
});
【问题讨论】:
-
没有。但我使用汇总能够使用 es2015 模块/导入
-
您应该尝试使用 webcomponents-lite。这里的错误表明当元素实例化时 polyfill 尚未加载。此外,您不应在 WebComponentsReady 回调中调用 customElement.define。
-
那么我应该在哪里调用定义?我认为在 WebComponentsReady 中调用它可以确保在完全加载 polyfill 后调用它
-
在加载了 polyfill 之后,或者在导入的文件中(使用 )。 WebComponentReady 确保组件已被定义和实例化,以便调用其方法。
-
polyfill 应该同步加载。我已经用 webcomponents-lite 对其进行了测试,它在 firefox 上运行良好。无需活动。只需将
标签: javascript ecmascript-6 web-component polyfills custom-element