I implemented a WebComponent in TypeScript that looks like this and is bundled via a WebPack:
class MyTestComponent extends HTMLElement {
// ...
connectedCallback() {
this.config = JSON.parse(this.innerText);
// ...
customElements.define("my-test", MyTestComponent);
In the web page it is used like this:
<my-test>
{
"parts": [
[3, "red"]
]
}
</my-test>
This worked fine using my WebPack genrated test page, but failed if I included the component into another page. I spend some time until I figured out that the defer
attribute at the including script tag makes the difference. If I use defer
the component works fine. If not this.innerText
is empty when connectCallback
is called.
I would like to understand why defer
makes a difference and what's going on in detail. Can somebody explain?