【问题标题】:ActivatedRoute params is empty in CanActivate guardCanActivate 防护中的 ActivatedRoute 参数为空
【发布时间】:2019-12-30 12:56:19
【问题描述】:

我有许多应用程序的通用防护:

export class Guard implements CanActivate {
    constructor(private authService: AuthService, private router: Router, private route: ActivatedRoute) {}
    async canActivate(route: ActivatedRouteSnapshot) {
        const user = await this.authService.getUserAuthenticated();
        const hasPerm = user && user.hasPermission(route.data.app);
        if (!hasPerm) {
            this.router.navigate(['/login/' + this.route.snapshot.params.site, {}]);
        }
        return true;
    }
}

this.route.snapshot.params.site 的路由为空:"/signin/macro/561"

路线声明:

{ path: 'signin/:site/:token', loadChildren: 'src/app/auth/auth.module#AuthModule' },

【问题讨论】:

  • 你在路线上没有设置任何守卫

标签: angular angular-routing angular-guards


【解决方案1】:

尝试使用 ActivatedRoute 并以this.activatedRoute.snapshot.params.site 访问参数。 另外,您正在使用 ActivatedRouteSnapshot 并尝试访问它的快照属性,这显然是未定义的,因为它没有任何此类属性。在这种情况下,您可以尝试直接访问参数。

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 2020-12-18
    • 2020-12-28
    • 1970-01-01
    • 2017-08-14
    • 2017-03-13
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多