How can I make a web component using an external HTML file?

526 Views Asked by At

I want my HTML code for my web component to be in its own file, and therefore I would import it into the JS for my web component. I know I can use the <link> tag to do this, but that's obsolete now. How can I make a web component while having the HTML be in its own file, similar to Angular components?

Currently, I use fetch to fetch it, but that requires me to convert it into a string, then attach it to the DOM, and isn't pleasant when having multiple components. It works for now but doesn't seem right.

I read, but forgot where, I can do something like this:

import * as template from "my-component.html";

I tried it but I received this error:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Is there any normal way to import an HTML file?

0

There are 0 best solutions below