Thursday, August 26, 2021

Angular - What are entryComponents

This is for dynamically added components that are added using ViewContainerRef.createComponent(). Adding them to entryComponents tells the offline template compiler to compile them and create factories for them.

The components registered in route configurations are added automatically to entryComponents as well because router-outlet also uses ViewContainerRef.createComponent() to add routed components to the DOM.

Offline template compiler (OTC) only builds components that are actually used. If components aren't used in templates directly the OTC can't know whether they need to be compiled. With entryComponents you can tell the OTC to also compile this components so they are available at runtime.


If you don't list a dynamically added component to entryComponents you'll get an error message a bout a missing factory because Angular won't have created one.


A Bit of Background about entryComponent


entryComponent is any component Angular loads imperatively. You can declare entryComponent by bootstrapping it in NgModule or in route definitions.


@NgModule({

  declarations: [

    AppComponent

  ],

  imports: [

    BrowserModule,

    FormsModule,

    HttpClientModule,

    AppRoutingModule

  ],

  providers: [],

  bootstrap: [AppComponent] // bootstrapped entry component

})



References:

https://stackoverflow.com/questions/39756192/what-is-entrycomponents-in-angular-ngmodule





No comments:

Post a Comment