我的RC5应用程序中出现此错误:
Promise rejection: Bootstrap at least one component before injecting Router.
main.ts:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule);
app.module.ts:
@NgModule({ imports: [ BrowserModule,routing,SharedModule.forRoot() ],declarations: [ AppComponent,ErrorComponent ],bootstrap: [AppComponent] }) export class AppModule {}
shared.module.ts:
@NgModule({ imports: [CommonModule,RouterModule,HttpModule,FormsModule,ReactiveFormsModule],declarations: [TranslatePipe,DateToStringPipe,HeaderComponent,FooterComponent],exports: [TranslatePipe,CommonModule,ReactiveFormsModule,FooterComponent] }) export class SharedModule { static forRoot(): ModuleWithProviders { return { ngModule: SharedModule,providers: [ FeedbackService,CookieService,AuthService,LoggerService,RouteGuard,{ provide: TranslateLoader,useFactory: (http: Http) => new TranslateStaticLoader(http,'app/languages','.json'),deps: [Http] },TranslateService,SearchService,SharedComponent,FooterComponent ] }; } } @NgModule({ exports: [ SharedModule],providers: [] }) export class SharedRootModule {}
app.component.ts:
export class AppComponent extends SharedComponent implements OnInit { constructor( _FeedbackService: FeedbackService,_loggerService: LoggerService,_translateService: TranslateService,_authService: AuthService,_router: Router ) { super( _FeedbackService,_loggerService,_translateService,_authService,_router ); }
最后是shared.component.ts:
constructor( protected _FeedbackService: FeedbackService,protected _loggerService: LoggerService,protected _translateService: TranslateService,protected _authService: AuthService,protected _router: Router ) { }
我尝试使用没有SharedComponent扩展的AppComponent,如下所示:
export class AppComponent implements OnInit { constructor(){ }
但这仍然会产生同样的问题.
Angular认为没有必要在模块级注入Router,在加载至少一个组件后注入Router是合理的.我怀疑至少有一个服务必须注入路由器,路由器提供给加载的模块,这会导致此错误.您可以做的是将使用路由器的服务注入应用程序组件,因此首先加载至少一个组件,并且所有子组件将从应用程序组件继承该服务.