【发布时间】:2017-09-14 12:39:27
【问题描述】:
第一个组件:x-input。一个什么都不做的非常简单的输入组件:
(function() {
class XInput extends HTMLElement {
static get template() {
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: block;
}
</style>
<input id="input">
`;
return template;
}
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(XInput.template.content.cloneNode(true));
}
}
window.customElements.define('x-input', XInput);
})();
我的组件在一个 .js 文件中,所以我不需要在我的客户端页面中使用 HTML 导入。 现在,我需要第二个从 x-input 继承的 Web 组件:
(function() {
class XInputNumber extends XInput {
}
window.customElements.define('x-input-number', XInputNumber);
})();
如果我尝试在页面中使用第二个元素 -> Uncaught ReferenceError: XInput is not defined.
<html>
<head>
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<script src="/bower_components/shadydom/shadydom.min.js"></script>
<script src="/webcomponents/input/x-input.js"></script>
<script src="/webcomponents/input/x-input-number.js"></script>
</head>
<body>
<p><x-input></x-input></p>
<p><x-input-number></x-input-number></p>
</body>
</html>
如果我在 html 文件中编写我的 web 组件,没问题,但我必须使用 HTML 导入(我尝试不使用它)。
我怎样才能做到这一点?
【问题讨论】:
-
JS 类仅在当前上下文范围内可见。也许this question 会有所帮助。另请查看
export。 -
确实,我的对象进入了 IIFE。如果我删除这个 IIFE,我的例子就有效。但它只有效,因为我在标题中声明了我的两个 Web 组件。如果我只想在我的页面中使用第二个 Web 组件(所以我只声明了这个),它不会工作(依赖的 XInput 没有在任何地方声明......
-
我不确定你是否注意到我提到的
export。目前尚不清楚您的平台要求是什么。您可能可以使用export default class测试您的代码。一些最新版本的浏览器应该支持它。 -
我的平台要求是IE11+,所以什么都用不了……
标签: javascript inheritance web-component custom-element