Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...