admin管理员组文章数量:1325236
is it possible to declare an array and use that to bootstrap multiple ponents in module.ts. I was trying something like this
export var initialComponents = [];
initialComponents.push(AppComponent);
if(condition) {
initialComponents.push(IchFooterComponent);
}
and then
bootstrap: initialComponents
Which gives me the following error
Error: The module oa was bootstrapped, but it does not declare "@NgModule.bootstrap" ponents nor a "ngDoBootstrap" method. Please define one of these.
is it possible to declare an array and use that to bootstrap multiple ponents in module.ts. I was trying something like this
export var initialComponents = [];
initialComponents.push(AppComponent);
if(condition) {
initialComponents.push(IchFooterComponent);
}
and then
bootstrap: initialComponents
Which gives me the following error
Share Improve this question asked Jul 10, 2018 at 7:31 Sahasrangshu GuhaSahasrangshu Guha 6832 gold badges13 silver badges30 bronze badges 2Error: The module oa was bootstrapped, but it does not declare "@NgModule.bootstrap" ponents nor a "ngDoBootstrap" method. Please define one of these.
-
Have you declared a @ngModule?
@NgModule({ declarations: initialComponents | Array<Type<any> | any[], bootstrap: Array<Type<any> | any[] })
– Gary Commented Jul 10, 2018 at 7:47 - yeah i declared ng module – Sahasrangshu Guha Commented Jul 10, 2018 at 7:52
2 Answers
Reset to default 7You can customize the bootstrapping via implementing ngDoBootstrap
as method of AppModule
.
You can list your ponents which need to be bootstrapped in the entryComponents
property of @NgModule
@NgModule({
entryComponents: [AComponent, BComponent, ...]
...
})
export class AppModule {
constructor() {}
ngDoBootstrap(appRef: ApplicationRef) {
if (Math.random() > 0.5) {
appRef.bootstrap(AComponent, '#app');
} else {
appRef.bootstrap(BComponent, '#app');
}
}
If you need a service, you can access them via dependency injection (put it in AppModule as constructor parameter). But I don't know if there are any limitations to it pared to DI in ponents or services. Here are the docs for ApplicationRef and its bootstrap method.
Try it again (below is the sample code):
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { AppComponent } from './app.ponent';
import { HeroesComponent } from './heroes/heroes.ponent';
var initialComponents: any[] = [];
initialComponents.push(AppComponent);
if (true) {
initialComponents.push(HeroesComponent);
}
@NgModule({
declarations: initialComponents,
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Try this again:
https://stackblitz./edit/angular-vimqb1?file=src/app/app.module.ts
本文标签: javascriptBootstrapping multiple module in Angular 6 via arrayStack Overflow
版权声明:本文标题:javascript - Bootstrapping multiple module in Angular 6 via array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167524a2426104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论