简体   繁体   English

Angular 2 路线:解决不要求儿童

[英]Angular 2 routes: resolve not called for children

Using:使用:

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

Routes look like:路线看起来像:

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

The issue is that resolve is not triggering for any direct navigation to a child path.问题是解析不会触发任何直接导航到子路径。 Ex: if user navigates directly to /login, PrefetchProfileService is not called.例如:如果用户直接导航到 /login,则不会调用 PrefetchProfileService。 If user navigates to /, then goes to /login, everything is fine.如果用户导航到 /,然后转到 /login,一切都很好。 How do I deal with this correctly?我该如何正确处理?

You have to add resolve in each child route you want to use PrefetchProfileService.您必须在要使用 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]}

,

你也可以只在你的父路由上使用 runGuardsAndResolvers: "always"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM