0
votes

I implement a role guard in my Angular 11 project, when i was coding i got this doubt:

Why when inject ActivatedRoute in constructor i didn't get route data?

code:

constructor(private route: ActivatedRoute) {}

canActivate(): boolean {
  this.route.data.subscribe(res => console.log(res));
  return true;
}

console:

{}

But, if i pass the ActivatedRoute by params through canActivate(), i get them

code:

constructor() {}

canActivate(route: ActivatedRoute): boolean {
  console.log(route.data);
  return true;
}

console:

{role: Array(2)}

why happen this?

1

1 Answers

0
votes

The Angular CanActivate guard decides, if a route can be activated, this is because canActivate looks for all guards return true for navigation to continues.

If any guard returns a UrlTree ( route serialized state ), the current navigation is cancelled and a new navigation begins to the UrlTree returned from the guard.

More Info here