【问题标题】:Can I read route parameter's value from the `canActivate: AuthGuard`?我可以从 `canActivate: AuthGuard` 中读取路由参数的值吗?
【发布时间】:2019-02-26 21:30:15
【问题描述】:
{
    path: ':l/bla/bla/bla', component: ..., canActivate: [AuthGuard]
}

然后AuthGuard的构造函数:

constructor(
    private readonly router: Router,
    private readonly activedRoute: ActivatedRoute) { }

...但是.params.snapshot.params 都是空的,并且不能从那里读取:l 参数的值(“语言”的缩写)。

所以问题:如果我的 dynamic :l 就位,如何在未经授权的访问的情况下重定向到另一个页面?


附: data 不起作用(是吗?),因为它可以用于编译时已知数据而不是动态数据。

【问题讨论】:

标签: angular angular-routing angular-router-guards angular-route-guards


【解决方案1】:
canActivate(activatedRouteSnapshot: ActivatedRouteSnapshot) { ... }

解决了这个问题:方法参数应该优先于构造函数注入。

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2017-12-15
    • 2019-02-12
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多