简体   繁体   English

JavaScript:参考匿名 class static 变量

[英]JavaScript: refer to anonymous class static variable

Following this google tutorial on web components , and I like the way the author uses an anonymous class in the customElements.define function call, as it avoids even more brackets when creating an immediately invoked function call. Following this google tutorial on web components , and I like the way the author uses an anonymous class in the customElements.define function call, as it avoids even more brackets when creating an immediately invoked function call.

I am using a static class variable to store the template element, so it's not queried every time the custom tag is instantiated.我正在使用static class 变量来存储模板元素,因此每次实例化自定义标签时都不会查询它。

The problem is in connectedCallback I don't know how to refer to class name since it is an anonymous function.问题出在connectedCallback我不知道如何引用 class 名称,因为它是一个匿名的 function。 In python one could to type(self) , but my research shows that typeof this does not work in JS due to prototype magic.在 python 中,可以使用type(self) ,但我的研究表明typeof this在 JS 中不起作用。

window.customElements.define('my-uploader', class extends HTMLElement {
    static template = document.getElementById("TEMPLATE_UPLOADER");

    constructor() {
        super(); // always call super() first
    }

    /** Custom Component Reactions **/
    connectedCallback() {
        let shadowRoot = this.attachShadow({mode: "open"});
        shadowRoot.appendChild(document.importNode(ANONYMOUS_NAME.template, true));  // <--- How to refer to the class name here?
    }

How to refer to the class' ANONYMOUS_NAME in connectedCallback ?如何在connectedCallback中引用班级的ANONYMOUS_NAME

You can either name your class, it's the cleanest way, or you can use this.constructor to get your static variable value:您可以命名您的 class,这是最干净的方式,或者您可以使用this.constructor获取 static 变量值:


window.customElements.define('my-uploader', class extends HTMLElement {
    static template = document.getElementById("TEMPLATE_UPLOADER");

    constructor() {
        super(); // always call super() first
    }

    /** Custom Component Reactions **/
    connectedCallback() {
        let shadowRoot = this.attachShadow({mode: "open"});
        shadowRoot.appendChild(document.importNode(this.constructor.template, true)); 
    }

When working with shadowDOM you (in general) create everything in the constructor使用 shadowDOM 时,您(通常)在constructor中创建所有内容

The connectedCallback triggers for every DOM insertion OR DOM CHANGE, Think Drag/Drop interactions. connectedCallback触发每个 DOM 插入或 DOM CHANGE,Think Drag/Drop 交互。 or sorting DOM Elements.或排序 DOM 元素。
It will error if you try to attach a shadowRoot again如果您再次尝试附加shadowRoot,它将出错

You can chain all JS, see below你可以链接所有的JS,见下文

importNode is for multiple Documents; importNode用于多个文档; you will see cloneNode in the better blogs.您将在更好的博客中看到cloneNode

append has more power, see the docs. append具有更大的功率,请参阅文档。
It just wasn't available in IE, so old JS-Geezers don't know about it.它只是在 IE 中不可用,所以老 JS-Geezers 不知道它。

 customElements.define('my-uploader', class extends HTMLElement { constructor() { let template = document.getElementById("TEMPLATE_UPLOADER").content; // don't believe the (MDN) documentation, you CAN execute JS BEFORE super() // you just can't access 'this' BEFORE super() super() // super sets AND returns the 'this' scope.attachShadow({mode: "open"}) // sets AND returns this.shadowRoot; .append(template.cloneNode(true)); } });
 <template id="TEMPLATE_UPLOADER"> <style>:host{ color:red; } </style> <h1>I am <slot></slot></h1> </template> <h1>Hello Web Components!</h1> <my-uploader>Uploader!</my-uploader>

Note!笔记! The constructor also runs on document.createElement("my-uploader") when your Element DOES NOT EXIST in DOM yet!当您的元素在 DOM 中尚不存在时, constructor也会在document.createElement("my-uploader")上运行! Only when added to the DOM the connectedCallback runs仅当添加到 DOM 时, connectedCallback才会运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM