相关疑难解决方法(0)

自定义HTMLElement的connectedCallback()中的textContent为空

connectedCallback()我的自定义元素的方法中,textContent它作为空字符串返回。

本质上,我的代码可以归结为以下内容...

class MyComponent extends HTMLElement{
    constructor() {
        super()

        console.log(this.textContent) // not available here, but understandable
    }           

    connectedCallback() {
        super.connectedCallback() // makes no difference if present or not

        console.log(this.textContent) // not available here either, but why?!
    }
}

customElements.define('my-component', MyComponent);     
Run Code Online (Sandbox Code Playgroud)

还有HTML ...

<my-component>This is the content I need to access</my-component>
Run Code Online (Sandbox Code Playgroud)

从阅读方面看,connectedCallback()这听起来好像是在将元素添加到DOM后就被调用了,所以我希望textContent属性应该是有效的。

我正在使用Chrome 63,如果有帮助...

javascript web-component web custom-element native-web-component

2
推荐指数
3
解决办法
544
查看次数