2016-11-30 23 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; 
    }