gpt4 book ai didi

javascript - ES6 自定义元素继承

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:25 25 4
gpt4 key购买 nike

我正在尝试在自定义元素中使用继承。

那是我的页面

<!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 的情况下工作,对吗?我的问题是控制台中的这条消息

未捕获的类型错误:类扩展值 [object Module] 不是构造函数或 null

我不明白我哪里错了...有人有想法吗?

所有这些都是因为我想避免使用 HTML 导入,因为它已被弃用并将在 M73 中删除。

如果我用

<script src="./custom-elements/BaseHTMLElement.js"></script>
<link rel="import" href="./custom-elements/language-drop-down/LanguageDropDown.html">

并删除 导出默认的 BaseHTMLElement ... import * as BaseHTMLElement from '../../BaseHTMLElement.js';

它工作正常!

最佳答案

你的线路

import * as BaseHTMLElement from '../../BaseHTMLElement.js';

错了。该脚本将该类导出为其默认导出。您在此处将所有 导出作为模块 namespace 对象导入 - 当尝试像 class extends 子句中的类一样使用它时,您会得到“ [object Module] 不是构造函数或为 null”。

你应该使用

import {default as BaseHTMLElement} from '../../BaseHTMLElement.js';

import BaseHTMLElement from '../../BaseHTMLElement.js';

关于javascript - ES6 自定义元素继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53610411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com