2016-11-30 22 views
0

我想在某些情況下在頁面加載或之前重定向。例如,Cookies必須有一些東西,然後做一個重定向。如何從NgOnInit重定向?

this.router.navigate(["details"]); in AppComponent.NgOnInit not working!

路由器:

const APP_ROUTES : Route[] = [ 
    { path: '', component: FormComponent, children: FORM_ROUTES}, 
    { path: 'details', component: DetailsComponent} 
]; 

模塊導入和引導:

imports: [ 
BrowserModule, 
FormsModule, 
HttpModule, 
RouterModule.forRoot(APP_ROUTES), 
RouterModule.forChild(FORM_ROUTES) 
], 
bootstrap: [AppComponent] 

角2.

+2

您應該使用路由器逆天做有條件的重定向。 –

+1

「不工作」是什麼意思?你的路線是怎樣的。什麼組件包含上面的代碼? –

+0

@GünterZöchbauer已更新 –

回答

1

CanActivate決定了我的問題

路由器

{ path: '', component: FormComponent, children: FORM_ROUTES}, 
{ path: 'details', component: DetailsComponent, canActivate [NoAuthRedirectService]} 

NoAuthRedirectService

canActivate() : boolean 
    { 
    if(!this.auth.OnAuth()) 
    { 
     this.router.navigate(["/"]); 
     return false; 
    } 
    return true; 
    }