2

I am trying to set side bar routes based on different user role privileges. The code for setting side bar routes is in site-resolver service. Which I have called in parent path which I think should be also called for its child routes. But instead, this resolver is getting called only when I refresh the particular child route.

Here's the example of my site-routing service:

{
path: ':siteId',
resolve: { site: SiteResolverService },
children: [
  // ... other child routes
  {
    path: 'jsa-training-user-assigned-jsa',
    loadChildren: () => import('../jsa-quiz/jsa-quiz.module').then(m => m.JsaQuizModule),
    canActivate: [RoleGuardService],
    data: { expectedRole: [UserRole.SystemAdmin, UserRole.JsaCreator, UserRole.SiteAdmin, UserRole.PasscodeUser] },
    runGuardsAndResolvers: 'always'
  },
  // ... other child routes
]

here in this route i want the resolver to get called without refreshing, so i have tried below, but not working.

runGuardsAndResolvers: 'always'

Hope
  • 475
  • 6
  • 16
  • https://stackoverflow.com/questions/41496499/resolve-not-called-for-child-routes-in-angular-2 – hrdkisback Feb 12 '20 at 07:12
  • @hrdkisback yeah I checked that question before posting this one but that question didn't have any specific solution apparently.. – Hope Feb 12 '20 at 07:13
  • Please post `SiteResolverService` code. – hrdkisback Feb 12 '20 at 07:18
  • @hrdkisback but that isn't necessary until that resolver gets called. I put alert in that resolver's constructor and main thing is it isn't getting called. – Hope Feb 12 '20 at 08:05
  • 1
    what if you remove `resolve: { site: SiteResolverService }` from parent route and provide on each child routes. Refer this https://stackblitz.com/edit/angular6-lazy-loading-resolve-demo – hrdkisback Feb 13 '20 at 13:17
  • @hrdkisback yes as a last option I then used that! Thanks. – Hope Feb 14 '20 at 03:49

1 Answers1

1

The resolver is called when you refresh a child route because the whole route tree is triggered, while accessing a child route from a parent will only trigger the child route.

You can still get the result of the parent resolver in the child component by accessing the route.parent :

Child component.ts

import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute) {}

ngOnInit() {
  console.log(this.route.parent.snapshot.data);
}
Gérôme Grignon
  • 3,859
  • 1
  • 6
  • 18
  • That clears the doubt yeah! but I have set the routes in the resolver so I don't want any data from the resolver, I just want it to get triggered for the lazy loaded module given in my code. And that is module and not component. – Hope Feb 12 '20 at 12:34