Angular 2路由:解析没有为孩子调用

时间:2016-12-12 18:19:00

标签: angular angular2-routing

使用:

"@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。如果用户导航到/,然后转到/ login,一切都很好。我该如何正确处理?

2 个答案:

答案 0 :(得分:1)

您必须在要使用PrefetchProfileService的每个子路由中添加resolve。

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]}

,

答案 1 :(得分:0)

您也可以只在您的父路径上使用runGuardsAndResolvers:“ always”