主要问题在于我的AuthService(我将其注入到AppComponent中)注入了Router,而这个Router是提供给已加载的模块的,这导致出现了错误。我将它从Auth Service中删除,并将其留在AppComponent中,因此至少会先加载一个组件。
我目前正在尝试弄清楚新版本的Angular工作原理。
我遇到了以下错误:
在注入Router之前需要至少引导一个组件。
我的app.module看起来像这样:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import {HttpModule} from '@angular/http';
import {routing, APP_ROUTER_PROVIDERS, children } from './app.routes';
import {appStateProvider} from './providers/AppStateProvider'
// Import configured routes
import {AuthGuard} from './services/auth.guard'
import {AuthService} from './services/auth.service';
import {AppComponent } from './app.component';
import {LoginComponent} from './components/login.component'
import {HomeComponent} from './components/home.component'
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing,
children
],
providers: [
appStateProvider,
APP_ROUTER_PROVIDERS,
AuthService,
InsaService
],
declarations: [
AppComponent,
LoginComponent,
HomeComponent,
NewsComponent
],
bootstrap: [AppComponent]
})
export class AppModule { }
main.ts文件:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import 'rxjs/Rx';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
和应用程序组件:
import {Component, OnInit, OnDestroy} from '@angular/core'
import { Router,Routes,
NavigationExtras, ROUTER_DIRECTIVES } from '@angular/router';
import {Http} from '@angular/http';
import {AuthService} from './services/auth.service'
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
})
export class AppComponent implements OnInit, OnDestroy {
private authenticated: boolean = false;
private loginSubscriber;
constructor(private _appStateProvider: appStateProvider, private _authService: AuthService, private _insaService: InsaService, private _router:Router) {
console.log("AppComponent logged")
if (this._authService.isLoggedIn()) {
this._router.navigate(['/home']);
}
else {
this._router.navigate(['/login']);
}
}
ngOnInit() {
this.loginSubscriber = this._authService.LoggedInStatusChangedEmitter.subscribe(
(loggedin) => {
this.authenticated = loggedin.value;
}
);
}
logout(sender: any) {
this._authService.logout();
}
ngOnDestroy(){
this.loginSubscriber.unsubscribe();
}
}
我将应用程序更新到RC5,并按照文档进行操作,但不知道是什么原因导致了错误。