2016-07-05 7 views
0

を働いていない私は、私のルータを行うにはangular2 RC3とangular2 /ルータ3.0.0-alpha.8を使用します。 ここでは私のコードです:angular2ルートが

bootstrap(AppComponent, [HTTP_PROVIDERS, APP_ROUTER_PROVIDERS, AUTH_PROVIDERS, 
    { 
     provide: AuthHttp, 
     useFactory: (http:Http) => new AuthHttp(new AuthConfig({tokenName: 'jwt'}), http), 
     deps: [Http] 
    }, 
    { 
     provide: TranslateLoader, 
     useFactory: (http:Http) => new TranslateStaticLoader(http, 'app/i18n', '.json'), 
     deps: [Http] 
    }, 
    {provide: LocationStrategy, useClass: PathLocationStrategy}, 
    // use TranslateService here, and not TRANSLATE_PROVIDERS (which will define a default TranslateStaticLoader) 
    TranslateService, 
    ApiService, 
    CookieService, 
    AuthenticationService 

]).catch(err => console.error(err)); 

私のルータは次のとおりです。

export const routes:RouterConfig = [ 
    ...AccountRouters, 
    ...DealOverviewRouters 

]; 

export const APP_ROUTER_PROVIDERS = [ 
    provideRouter(routes) 
]; 


export const AccountRouters: RouterConfig = [ 
    { 
     path: 'account', component: AccountComponent 
    }, 
    { 
     path: 'login', component: LoginComponent 
    }, 
]; 

export const DealOverviewRouters:RouterConfig = [ 
    { 
     path: '', 
     redirectTo: '/deal-overview', 
     terminal: true 
    }, 
    { 
     path: 'deal-overview', component: DealOverviewComponent 
    } 
]; 
その後、私のapp.component.tsで

constructor(public translate:TranslateService, private _service: AuthenticationService, private _router: Router) { 
     this.isLogin = _service.isLogin; 
     console.log(_service.isLogin); 
} 

ngOnInit() { 
     // this._service.checkCredentials(); 
     if(!this.isLogin) { 
      console.log(1111); 
      this._router.navigateByUrl('/login'); 
     } 
} 

それは本当に私のコンソールログに1111を印刷しています。 ですが、/loginへのリダイレクトは機能しません。

localhost:3000/loginページに直接アクセスできますが、何もエラーはありません。 ですが、this._router.navigateByUrl('/login')は機能しません。 1つのconstにそれらすべてを配置して、それをしようと手動で、代わりに

export const routes:RouterConfig = [ 
    ...AccountRouters, 
    ...DealOverviewRouters 
]; 

+0

あなたは '持っていますか'index.html'の' 'タグの'? - ** '/'は重要です** –

答えて

1

私は新しいルータでは、次の構文を使用して問題を持っていた

export const routes:RouterConfig = [ 
    { 
     path: '', 
     redirectTo: '/deal-overview', 
     terminal: true 
    }, 
    { 
     path: 'deal-overview', component: DealOverviewComponent 
    }, 
    { 
     path: 'account', component: AccountComponent 
    }, 
    { 
     path: 'login', component: LoginComponent 
    } 
]; 
関連する問題