Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/65903908/why-happen-this-with-angular-activatedroute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
791 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...