2016-12-12 71 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

謝謝,這確實有效,但是對於很多路線來說都是非常煩人的。這也違背了警衛的工作方式(從最頂層的父母開始並行應用)。我最終做的預防與警衛,而不是:( –