首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将激活路径快照转换为激活路由-Angular 9

将激活路径快照转换为激活路由-Angular 9
EN

Stack Overflow用户
提问于 2020-06-25 01:27:23
回答 1查看 456关注 0票数 1

我正在研究角度为9的警卫的canActivate函数,我想相对于路径在保护中导航。问题是我需要将ActivatedRouteSnapshot转换为ActivatedRoute

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-26 12:06:57

在最新版本(14.*)中包含了createUrlTreeFromSnapshot函数,因此现在您可以直接使用ActivatedRouteSnapshot了:

代码语言:javascript
复制
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://github.com/usarskyy/circulardependency/blob/master/src/app/auth/feature/auth-shell/guards/can-login.guard.ts

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62566466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档