【问题标题】:Get bootstraped component获取引导组件
【发布时间】:2016-09-27 06:54:31
【问题描述】:

我有一个简单的@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 { }

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

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

有什么想法吗?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

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

    1) 使用ApplicationRef

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

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

    【讨论】:

    • 谢谢!我选择第一个解决方案。
    • 如何在 main.ts @ChristopheGigax 中加载 NgModuleRef 和 A​​pplicationRef
    • @ChristopheGigax,你能分享我加载上述程序的代码吗?
    • 我没有代码了,抱歉。那是很久以前。也许你可以通过bootstrapModule方法的then方法找到NgModuleRef
    • @ChristopheGigax,感谢您的回复。你能以任何方式帮助你如何编写它,在哪个文件中,如 main.ts 或模块文件。正如我已经尝试过的那样,但这两种解决方案都不起作用。如果可能的话,试着记住并帮助我。
    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-05-28
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多