Get bootstraped component

前端 未结 1 1423
無奈伤痛
無奈伤痛 2021-01-14 13:54

I have a simple @NgModule the boostrap 2 components :

// Global module for all Angular Component
@NgModule({
    declarations: Components, // di         


        
相关标签:
1条回答
  • 2021-01-14 14:32

    I see two ways to get list of bootstraped components:

    1) Using ApplicationRef

    platformBrowserDynamic()
        .bootstrapModule(MenuModule)
        .then((menuModule: NgModuleRef<MenuModule>) => {
            const appRef = menuModule.injector.get(ApplicationRef);
            const components = appRef.components;      
        });
    

    2) Using ngDoBootstrap method on your module:

    @NgModule({
        declarations: [Components, // directives, components, and pipes owned by this NgModule
        imports: [BrowserModule, HttpModule, FormsModule],
        providers: Providers,
        entryComponents: [Menu, Main]
        // bootstrap: [Menu, Main]
    })
    export class MenuModule {
       ngDoBootstrap(appRef: ApplicationRef) {
         const compRefMenu = appRef.bootstrap(Menu);
         const compRefMain = appRef.bootstrap(Main);
       }
    }
    
    0 讨论(0)
提交回复
热议问题