Javascript 通过阵列引导Angular 6中的多个模块
是否可以声明一个数组并使用它来引导module.ts中的多个组件。 我在尝试这样的事情Javascript 通过阵列引导Angular 6中的多个模块,javascript,angular,typescript,frontend,angular6,Javascript,Angular,Typescript,Frontend,Angular6,是否可以声明一个数组并使用它来引导module.ts中的多个组件。 我在尝试这样的事情 export var initialComponents = []; initialComponents.push(AppComponent); if(condition) { initialComponents.push(IchFooterComponent); } 然后 bootstrap: initialComponents 这给了我以下的错误 错误:模块
export var initialComponents = [];
initialComponents.push(AppComponent);
if(condition) {
initialComponents.push(IchFooterComponent);
}
然后
bootstrap: initialComponents
这给了我以下的错误
错误:模块oa已引导,但未声明“@NgModule.bootstrap”组件或“ngDoBootstrap”方法。请定义其中一个
请重试(下面是示例代码):
从'@angular/platform browser'导入{BrowserModule};
从“@angular/core”导入{NgModule};
从“@angular/forms”导入{FormsModule};// 您可以通过实现ngDoBootstrap
作为AppModule
的方法来定制引导。
您可以在@NgModule
@NgModule({
entryComponents: [AComponent, BComponent, ...]
...
})
export class AppModule {
constructor() {}
ngDoBootstrap(app: ApplicationRef) {
if (Math.random() > 0.5) {
appRef.bootstrap(AComponent, '#app');
} else {
appRef.bootstrap(BComponent, '#app');
}
}
如果您需要一个服务,您可以通过依赖项注入(将其作为构造函数参数放入AppModule)来访问它们。但我不知道与DI相比,它在组件或服务方面是否有任何限制。
以下是的文档及其引导方法。您是否声明了@ngModule<代码>@NgModule({声明:initialComponents | Arrayyeah我声明了ng模块我需要在引导程序中声明多个组件,其中只有很少的组件会根据条件出现。您的示例是先加载应用程序组件,然后从应用程序内部加载heroes组件,这不是我的用例。请尝试将所有内容都放入条件或外部co条件它应该可以工作。这是一种数组类型的声明。据我所知,无论它以何种方式工作。节省了我的时间!引导:[condition?ComponentOne:ComponentTwo]在开发模式下工作,但ng build--prod崩溃:“NgModule中位置0处的值。AppModule的引导不是引用:[object object”。删除“引导”并通过ngDoBootstrap实现后,构建和工作正常。不幸的是,装饰器(如@NgModule
)内容(如引导)非常脆弱,需要“非常静态”,尤其是在构建情况下,当使用大量角度优化时)
@NgModule({
entryComponents: [AComponent, BComponent, ...]
...
})
export class AppModule {
constructor() {}
ngDoBootstrap(app: ApplicationRef) {
if (Math.random() > 0.5) {
appRef.bootstrap(AComponent, '#app');
} else {
appRef.bootstrap(BComponent, '#app');
}
}