每隔一段时间,在构建我的应用时,我就会收到这个奇怪的问题:
EXCEPTION:在实例化路由器期间出错! (NavComponent - > Auth - >路由器)。 原始例外:路由配置应该只包含一个"组件"," loader"或" redirectTo"属性。
这很奇怪,因为它只是非常紧张地出现,而不是每次出现。
我的NavComponent
使用Auth
,并且RouterLink
。这是:
import {Component} from 'angular2/core';
import {NgIf} from 'angular2/common';
import {RouterLink} from 'angular2/router';
import {Auth} from '../auth.injectable'
@Component({
selector: 'navigation',
templateUrl: 'app/navigation/Navigation.html',
directives: [NgIf, RouterLink]
})
export class NavComponent{
private user:any;
private isLogged:boolean;
constructor(private _auth:Auth) {
this.user = _auth.getUser();
if(this.user.username){
this.isLogged = true;
}
}
logout(){
this._auth.logout();
}
}
我的Auth
服务使用@Inject(Router) private _router
,看起来像这样:
import {Inject} from 'angular2/core';
import {Http} from 'angular2/http';
import {Router} from "angular2/router";
import {EventEmitterService} from "../common/EventEmitter.service";
export class Auth {
public user: any = sessionStorage.getItem('user') ? JSON.parse(sessionStorage.getItem('user')) : {};
constructor(@Inject(Http) private _http, @Inject(Router) private _router, @Inject(EventEmitterService) private _eventEmitterService){
_eventEmitterService.logout.subscribe((data)=>{
_router.navigate(['Login']);
})
}
login(username,password){
var user = {
username: username,
password: password
};
this._http.post('/authentication/login',user)
.subscribe(
success =>{
this.user = success.json();
sessionStorage.setItem('user', JSON.stringify(success.json()));
this._router.navigate(['Dashboard']);
},
error =>{
this.user.authError = true;
});
}
getUser(){
return this.user;
}
}
我在Boot.js
:
bootstrap(MyApp, [
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
Auth,
provide(LocationStrategy, {useClass: HashLocationStrategy}),
provide(APP_BASE_HREF, {useValue: '/'})
....])
这是我的@RouteConfig
:
@RouteConfig([
{path: '/', redirectTo: ['Dashboard']},
{path: '/login', name: 'Login', component: LoginComponent},
{path: '/dashboard', name: 'Dashboard', component: DashboardComponent}
])
有什么想法吗?它应该每次都有效,而不是99%的时间。