获取引导组件 [英] Get bootstraped component

查看:88
本文介绍了获取引导组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple @NgModule the boostrap 2 components :

// 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 { }

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

I want to store a reference of my Main component bootstraped by my MenuModule. I tried to implement the .then method of bootstrapModuleDynamic(), but I only get the factory of the component.

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

有什么想法吗?

推荐答案

我看到两种获取自举组件列表的方法:

I see two ways to get list of bootstraped components:

1)使用ApplicationRef

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

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

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

这篇关于获取引导组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆