2016-12-12 55 views
0

使用:角2路:解决不叫孩子

"@angular/core": "2.2.2", 
"@angular/router": "3.2.2", 

路线是这样的:

export const rootRouterConfig: Routes = [ 
    { 
    path: '', resolve: { profile: PrefetchProfileService }, 
    children: [ 
     {path: '', component: Pages.LandingComponent, pathMatch: 'full'}, 
     {path: 'pl', component: PersonalLoanComponent}, 
     {path: 'login', component: Pages.LoginComponent, canActivate: [ExistingSessionGuard]}, 
     {path: 'register', component: Pages.RegisterComponent, canActivate: [ExistingSessionGuard]}, 
     {path: 'home', component: Pages.HomeComponent, canActivate: [AuthGuard]}, 
    ] 
    } 
]; 

的问题是,决心并未触发任何直接导航到子路径。例如:如果用户直接导航到/ login,则不会调用PrefetchProfileService。如果用户导航到/,然后去/登录,一切都很好。我如何正确处理这个问题?

回答

1

您必须在您要使用PrefetchProfileService的每个子路径中添加解析。

path: '', resolve: { profile: PrefetchProfileService }, 
     children: [ 
      {path: '', component: Pages.LandingComponent,resolve: { profile: PrefetchProfileService } ,pathMatch: 'full'}, 
      {path: 'pl', resolve: { profile: PrefetchProfileService },component: PersonalLoanComponent}, 
      {path: 'login', resolve: { profile: PrefetchProfileService },component: Pages.LoginComponent, canActivate: [ExistingSessionGuard]}, 
      {path: 'register', resolve: { profile: PrefetchProfileService },component: Pages.RegisterComponent, canActivate: [ExistingSessionGuard]}, 
      {path: 'home',resolve: { profile: PrefetchProfileService } ,component: Pages.HomeComponent, canActivate: [AuthGuard]} 

, 
+0

谢谢,这确实有效,但是对于很多路线来说都是非常烦人的。这也违背了警卫的工作方式(从最顶层的父母开始并行应用)。我最终做的预防与警卫,而不是:( –