Я пытаюсь использовать наследование в пользовательских элементах.
Это моя страница
<!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);
И язык drop-вниз пользовательский элемент
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 он должен работать без компиляции / использования полизаполнения, верно?Моя проблема заключается в том, что это сообщение в консоли
Uncaught TypeError: Класс расширяет значение [объектный модуль] не является конструктором или пустым
Я не понимаю, где я ошибаюсь ... делаету кого-то есть идея?
Все это потому, что я хотел бы избежать использования импорта HTML, потому что он устарел и будет удален в M73.
Если я использую
<script src="./custom-elements/BaseHTMLElement.js"></script>
<link rel="import" href="./custom-elements/language-drop-down/LanguageDropDown.html">
И удалить экспорт по умолчанию BaseHTMLElement ... импортировать * как BaseHTMLElement из '../../BaseHTMLElement.js';
все работает отлично!