Web components, how to separate HTML from code

后端 未结 2 506
情歌与酒
情歌与酒 2021-01-26 18:06

I would like to know if it is possible (I am using ES6 and Typescript) to separate my HTML code from my components i.e. include the HTML code into a variable from another file.<

2条回答
  •  执念已碎
    2021-01-26 18:11

    You could use fetch(). It's an asynchronous function so you'll avec to use async/await or Promises.

    class MyComponent extends HTMLElement {
    
        async connectedCallback() {
            let res = await fetch( 'my-component.html' )
    
            this.innerHTML = await res.text()
        }
    
    }
    customElements.define( 'my-component', MyComponent )
    

提交回复
热议问题