【发布时间】:2021-04-27 05:53:19
【问题描述】:
在自定义元素被解析并添加到 DOM 之后,我是否可以访问我在构造函数时创建的自定义属性,例如:
const props = []
return new CustomElement(props)
// later in code
class CustomElement {
...
constructor(props) {
this.customProperty = props
}
toString() {
return this.outerHTML
}
...
}
// later in code
const ce = document.querySelector("custom-element")
console.log( ce.customProperty ) // []
【问题讨论】:
-
您需要确保您的自定义元素已正确注册
customElements.define('custom-element', CustomElement);。 -
显示代码的目的是关注问题的相关方面。当然我已经定义了元素。 @connexo