Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通用网络组件导入_Javascript_Html_Web Component - Fatal编程技术网

Javascript 通用网络组件导入

Javascript 通用网络组件导入,javascript,html,web-component,Javascript,Html,Web Component,我目前正在使用纯HTML和VanillaJS开发网络组件。目标是为公司提供组件的工作集合 <template id="x-intranet-alert"> <div class="alert"> <p></p> </div> </template> <script> class IntranetAlert extends HTMLElement { sta

我目前正在使用
纯HTML
VanillaJS
开发
网络组件。目标是为公司提供
组件的工作集合

<template id="x-intranet-alert">
    <div class="alert">
        <p></p>
    </div>
</template>

<script>
    class IntranetAlert extends HTMLElement {
        static get observedAttributes() {
            return ['message', 'type', 'close']
        }

        constructor() {
            super()

            this.innerHTML = document.currentScript.ownerDocument.querySelector('#x-intranet-alert').innerHTML
        }

        attributeChangedCallback(attr, oldValue, newValue) {
            switch(attr) {
                case 'type':
                    this.querySelector("div").className = `alert ${newValue}`
                    break

                // ...
            }
        }
    }

    customElements.define('intranet-alert', IntranetAlert);
</script>
但是如果我试图以编程方式创建它,我会出错,因为
document.currentScript.ownerDocument
document
而不是components文档

let alert = new IntranetAlert()
alert.setAttribute("type", "danger")
alert.setAttribute("message", "Component créé dynamiquement")

document.querySelector("#alert").appendChild(alert)

如何导入文档使其双向工作?

看看这个anwser:我觉得自己很笨,我在研究过程中没有发现这个问题。非常感谢!看看这个安瑟:我觉得自己很笨,我在研究中没有发现这个问题。非常感谢!
let alert = new IntranetAlert()
alert.setAttribute("type", "danger")
alert.setAttribute("message", "Component créé dynamiquement")

document.querySelector("#alert").appendChild(alert)