Angular 2 routes: resolve not called for children

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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. If user navigates to /, then goes to /login, everything is fine. How do I deal with this correctly?

回答1:

You have to add resolve in each child route you want to use 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]}  , 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!