我正在研究角度为9的警卫的canActivate函数,我想相对于路径在保护中导航。问题是我需要将ActivatedRouteSnapshot转换为ActivatedRoute。
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot){
if (allowRoute) {
return true;
} else {
this.router.navigate(['route'], { relativeTo: next });
//the error above is that relativeTo accepts a type of ActivatedRoute but next is ActivatedRouteSnapshot
return false;
}
}解决这个问题的最好方法是什么?是否有直接的方法将next转换为ActivatedRoute?
发布于 2022-10-26 12:06:57
在最新版本(14.*)中包含了createUrlTreeFromSnapshot函数,因此现在您可以直接使用ActivatedRouteSnapshot了:
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (localStorage.getItem('payed_100000_usd')) {
return true;
}
return createUrlTreeFromSnapshot(route, ['register']);
}https://stackoverflow.com/questions/62566466
复制相似问题