获取bootstraped组件

Chr*_*gax 3 typescript angular

我有一个简单@NgModule的boostrap 2组件:

// Global module for all Angular Component
@NgModule({
    declarations: Components, // directives, components, and pipes owned by this NgModule
    imports: [BrowserModule, HttpModule, FormsModule],
    providers: Providers,
    bootstrap: [Menu, Main]
})
export class MenuModule { }
Run Code Online (Sandbox Code Playgroud)

我想存储我的Main组件引导的引用MenuModule.我试图实现.then方法bootstrapModuleDynamic(),但我只获得组件的工厂.

// Bootstrap of module
platformBrowserDynamic()
    .bootstrapModule(MenuModule)
    .then((menuModule: NgModuleRef<MenuModule>) => {
        var test: ComponentFactory<Main> = (<any>menuModule).bootstrapFactories[0];
    });
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

yur*_*zui 5

我看到了两种获取bootstraped组件列表的方法:

1)使用 ApplicationRef

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

2)ngDoBootstrap在您的模块上使用方法:

@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);
   }
}
Run Code Online (Sandbox Code Playgroud)