What is entryComponents in angular ngModule?

前端 未结 6 1795
[愿得一人]
[愿得一人] 2020-12-04 06:31

I am working on an Ionic app ( 2.0.0-rc0 ) which depends on angular 2 . So the new introduction of ngModules is included.

6条回答
  •  猫巷女王i
    2020-12-04 06:58

    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
    })
    

    Documentation says below

    To contrast the two types of components, there are components which are included in the template, which are declarative. Additionally, there are components which you load imperatively; that is, entry components.

    Now to answer your specific question about entryComponents

    There is entryComponents array in @NgModule file. You can use this to add entryComponents if component is bootstrapped using ViewContainerRef.createComponent().

    That is you're creating components dynamically and not by bootstrapping or in template.

    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(myComp.component);
    const viewContainerRef = this.compHost.viewContainerRef;
    viewContainerRef.clear();
    const componentRef = viewContainerRef.createComponent(componentFactory);
    

提交回复
热议问题