【问题标题】:Angular2 Exploring Resolved Data in canActivate Guard?Angular2在canActivate Guard中探索解析的数据?
【发布时间】:2023-03-27 05:59:01
【问题描述】:

是否可以在 canActivate 保护中访问已解析的路由数据(-Resolver)。目前我可以通过

访问组件中的解析数据
ngOnInit() {
    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}

我如何在 canActivate 守卫中管理它?这不起作用:

constructor(private route: ActivatedRoute) {}

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot,
): boolean {

    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}

【问题讨论】:

  • 我希望data 仅在canActivate 返回true 后才可用
  • nope ... 手动设置路由data.example 通过使用this.route.data['example'] 使其在canActivate 中可用... 问题似乎是canActivate 在数据被解决之前运行resolver 并且发布的订阅不起作用
  • 您可以将保护逻辑移至解析中。但当然,这是一个不太优雅的解决方案。据我了解,所有解决方案都是可选的。您可以通过解析失败来阻止导航
  • 我有同样的问题,我有 authGuard 从数据中获取允许的角色列表:部分(工作正常)但我试图解决当前分配的用户角色以检查 authGuard 与“允许" 角色 - 数据部分(允许的角色列表)工作正常,但是我无法访问“已解决”的角色 - 甚至似乎都没有调用 resolve!

标签: angular router guard resolver


【解决方案1】:

不行,不能因为在resolve之前调用了canActivate方法,所以取不到数据

保护处理

  1. 可以停用

  2. 可以加载

  3. canActivateChild

  4. 可以激活

  5. 解决

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 2019-01-07
    • 2020-01-08
    • 2019-02-05
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多