I'm am using the latest version of Angular (11) to try and create an Angular Element that I will package up and use in a static webpage outside of an Angular app. However, while I'm developing the component, I would like to be able to use ng serve
or something similar to be able to load up my component in the browser and test out the features. What is the best way to accomplish this?
This is my current AppModule:
@NgModule({
declarations: [
ImageSwitcherComponent
],
imports: [
BrowserModule,
],
entryComponents: [
ImageSwitcherComponent // shouldn't need anymore
],
})
export class AppModule {
constructor(private injector: Injector) {
const el = createCustomElement(ImageSwitcherComponent, { injector: this.injector });
customElements.define('image-switcher', el);
}
}
I have deleted the default app component from the project, so my ImageSwitcherComponent
is the only component currently in the project. Additionally, no components are being bootstrapped, as I don't want to create a regular angular app.
Now, watching this tutorial video at the time linked, the guy says that with the above AppModule
configuration, you should be able to clear out the contents of the main index.html
(including getting rid of the app-root
) and just add the tag for the component element, run ng-serve
and your component should load:
<image-switcher></image-switcher>
Unfortunately, this does not seem to work, or I'm missing something. Nothing appears when loading localhost. And, when I run ng build --prod
, the generated index.html
file doesn't include any generated code beyond the tags shown above. The guy was using Angular 6, which is much older than what I'm using.
Is there something that has changed with more recent versions of Angular? Is there a better strategy for being able to serve up your Angular Elements in development and debug them in the browser?