我正在尝试在自定义元素中使用继承。
那是我的页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<base-element></base-element>
<language-drop-down></language-drop-down>
<script type="module" src="./custom-elements/BaseHTMLElement.js"></script>
<script type="module" src="./custom-elements/language-drop-down/js/LanguageDropDown.js"></script>
</body>
</html>
基本自定义元素
export default class BaseHTMLElement extends HTMLElement {
get currentDocument() { return document.currentScript.ownerDocument };
constructor(params) {
super();
this.params = params;
this.attachShadow({ mode: 'open' });
debugger
// Select the template and clone it. Finally attach the cloned node to the shadowDOM's root.
// Current document needs to be defined to get DOM access to imported HTML
if(this.params && this.params.templateName){
this.template = this.currentDocument.querySelector(this.params.templateName);
this.instance = this.template.content.cloneNode(true);
this.shadowRoot.appendChild(this.instance);
}else{
let div = document.createElement("DIV");
div.innerHTML = "element without template";
this.shadowRoot.appendChild(div);
}
}
connectedCallback() {
this.loadLocalStrings();
}
childrenChangedCallback() {
}
disconnectedCallback() {
}
attributeChangedCallback(attrName, oldVal, newVal) {
}
loadLocalStrings() {
}
}
window.customElements.define('base-element', BaseHTMLElement);
以及语言下拉自定义元素
import * as BaseHTMLElement from '../../BaseHTMLElement.js';
class LanguageDropDown extends BaseHTMLElement {
constructor() {
super({
templateName: '#language-drop-down-template'
});
}
connectedCallback() {
let dropdown = this.shadowRoot.querySelectorAll('.dropdown');
$(dropdown).dropdown();
}
childrenChangedCallback() {
}
disconnectedCallback() {
}
attributeChangedCallback(attrName, oldVal, newVal) {
}
};
window.customElements.define('language-drop-down', LanguageDropDown);
及其模板部分
<template id="language-drop-down-template">
<link href="./custom-elements/language-drop-down/css/languageDropDown.css" rel="stylesheet" />
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
</template>
<script type="module" src="../BaseHTMLElement.mjs"></script>
<script type="module" src="./js/languageDropDown.mjs"></script>
现在,在chrome最新版本中,无需编译/使用polyfill就可以正常工作吗? 我的问题是控制台中的此消息
未捕获的TypeError:类扩展值[object Module]不是构造函数或null
我不明白我哪里错了...有人有想法吗?
所有这些都是因为我想避免使用HTML导入,因为它已被弃用并将在M73中删除。
如果我使用
<script src="./custom-elements/BaseHTMLElement.js"></script>
<link rel="import" href="./custom-elements/language-drop-down/LanguageDropDown.html">
然后删除 导出默认的BaseHTMLElement ... 从'../../BaseHTMLElement.js'中导入*作为BaseHTMLElement;
工作正常!
答案 0 :(得分:3)
您的专线
import * as BaseHTMLElement from '../../BaseHTMLElement.js';
是错误的。该脚本将类导出为其默认导出。您要在此处将 all 导出作为模块名称空间对象导入-并在class extends
子句中尝试像类一样使用它时,会得到“ [object Module] is not a constructor or null
”。
您应该使用
import {default as BaseHTMLElement} from '../../BaseHTMLElement.js';
或
import BaseHTMLElement from '../../BaseHTMLElement.js';
答案 1 :(得分:1)
我发现使用
import BaseHTMLElement from '../../BaseHTMLElement.js';
使其有效