我已将自定义输入元素(表单的新式文本输入)设置为 Web 组件。我为实现它而制作的.js文件有三个部分。
模板:
const textInputTemplate = document.createElement('text-input-template');
textInputTemplate.innerHTML =
`
<div class="text-input-container">
<!--Irrelevant-->
</div>
`;
元素的类声明:
class textInput extends HTMLElement {
static get observedAttributes() {
return ['readonly'];
}
constructor () {
super();
// Shadow root
} // End of constructor()
connectedCallback () {
// Custom attributes
} // End of connectedCallback()
disconnectedCallback () {
// Remove event listeners
} // End of disconnectedCallback()
attributeChangedCallback(attribute, oldValue, newValue) {
// Updatable attributes: readonly
} // End of attributeChangedCallback()
}
最后,将自定义元素与标记名称关联的方法:
window.customElements.define('text-input', textInput);
问题:我担心使用效率低下或可能导致错误,因为它在页面的其余部分加载后同步加载。因此,我想知道是否有一种更清晰/更专业的方法来异步导入Web组件,而无需将整个模块粘贴到这样的函数中:<script src="./module-name">
export function textInput { // insert entire modules contents here }
因为我需要模块的所有三个部分才能使 Web 组件正常工作,所以我不能只导出 Web 组件类。
慕侠2389804
相关分类