【发布时间】:2016-08-10 09:46:27
【问题描述】:
主要问题是在我的 AuthService(我在 AppComponent 中注入)中注入了路由器,该路由器提供给加载的模块,这会导致此错误。我将其从 Auth Service 中删除并留在我的 AppComponent 中,所以在首先加载至少一个组件。
我目前正在尝试弄清楚新版本的 Angular 是如何工作的。 我收到了错误:
在注入路由器之前至少引导一个组件。
我的 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 并遵循文档,但不知道是什么导致了错误。
【问题讨论】:
-
我相信问题出在 AuthGuard 内部,您在其中注入了路由器。这里同样的问题。您找到任何解决方法了吗?
-
嗨!我更新了我的答案,请检查:)
-
没有清楚地得到您的答案,请您澄清您的解决方案
标签: angular typescript angular2-routing