I have created a simple web component using vanilla js just onclick to open URL on a new window unfortunately I get the following error
my-component.js:24 Uncaught TypeError: window.open is not a function
Here is my web component:
export class MyComponent extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this._render();
this._attachEventHandlers();
}
_render() {
const container = document.createElement("div");
container.innerHTML = `
<div class='app'>
<button id="open-me">Open</button>
</div>`;
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(container);
}
_attachEventHandlers() {
this.shadowRoot.getElementById('open-me').addEventListener('click', () => {
window.open('www.google.com', "_blank");
})
}
}
window.customElements.define("my-component", MyComponent);
usage of this component, just import in your HTML and add it like this
<my-component></my-component>
What is wrong here?