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

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

routes - Why happen this with Angular ActivatedRoute?

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...