我试图从 web 组件中引发事件,但确实如此。
<my-component id="xyz" bez="hallo" hello="myScript()"></my-component>
<script>
xyz.addEventListener("hello", function(event) {
console.log(event.detail.name);
});
</script>
html-tag "hello" 都不会引发事件,事件侦听器也不会。
Web 组件如下所示:
var button=document.createElement("button");
button.innerHTML=cap;
button.addEventListener('click', () => {
console.log("click");
button.dispatchEvent(new CustomEvent("hello", {
detail: { name: "John" }
}));
});
shadow.appendChild(button);
谁能帮我找出错误?非常感谢。
这里的代码小提琴:https ://jsfiddle.net/b43uqsLp/2/

