I’m trying to figure out how to use a web component inside another web component (both of which inherit from the same base class.
The problem seems to be in the way I’m creating the shadow dom:
import BaseElement from "./base.js";
class PopupHeader extends BaseElement {
constructor() {
super();
}
async render(initial = false) {
this.shadowRoot.innerHTML = `
<header>
<a href="https://example.com" id="logo" target="_blank"><img src="assets/logo.svg" alt="logo" /></a>
</header>
`;
}
async connectedCallback() {
await this.render(true);
}
}
customElements.define("popup-header", PopupHeader);
export default PopupHeader;
And base.js:
export default class BaseElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}
and main-app.js
:
import BaseElement from "./base.js";
class EasyApplier extends BaseElement {
constructor() {
super();
this.render = this.render.bind(this);
}
async render(initial = false) {
this.shadowRoot.innerHTML = `
<div id="${this.ns}">
<popup-header></popup-header>
Import profiles: <input type="file" id="json-file-input" accept="application/json" />
</div>
`;
}
...
}
The issue is the header is only shown, not the rest of the main components html.
I suspect it has to do with this.shadowRoot being overridden, is there a way to avoid this?
2
Answers
Just a quick glance:
${this.ns}
?? You are calling this in theconstructing
phase??Can’t it be simplified to:
I simplified your code for you. Don’t know why you think you needed async on render or why you sent in parameters you didn’t use.
Moved
connectedCallback()
that callsrender()
into your BaseElement.Note, snippets on Stackoverflow doesn’t require exports because it’s all in the same file. You still need those if each element is in it’s own separate file, but I suggest just having
export class SomeElement extends AnotherElement
instead of exporting on a single row.The problem could be that you’re trying to import from a relative path
from "./base.js"
instead of an absolute pathfrom "/base.js"
(no dot).