如何向守卫传递参数?
我有基于项目的 Angular 12。
我需要根据收集令牌的参数来保护路由:
export const authenticationRoutes = [
{
path: 'reset-password/token/:token',
component: ResetPasswordShellComponent,
canActivate: [ResetPasswordGuard]
}
];
ResetPasswordGuard 是一个调用 Web 服务的防护程序,并且应该发送作为路径一部分的令牌值:
path: 'reset-password/token/:token'
我的问题是有什么如何将令牌值从路径传递到守卫(ResetPasswordGuard),以便将其发送到Web服务?
I have project-based angular 12.
I need to secure a route based on a parameter which is colled token:
export const authenticationRoutes = [
{
path: 'reset-password/token/:token',
component: ResetPasswordShellComponent,
canActivate: [ResetPasswordGuard]
}
];
ResetPasswordGuard is a guard that makes calls to web service and should send token value which is part of the path:
path: 'reset-password/token/:token'
My question is there any way to pass token value from the path to guard(ResetPasswordGuard) so it will be sent to the web service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我们在
ResetPasswordGuard
上实现接口CanActiavte
时,函数canActivate
有 2 个参数,第一个是ActivatedRouteSnapshot
。 ..因此,人们可以使用以下方式轻松地从中读取路径参数...
使用
ActivatedRouteSnapshot
人们可以访问 URL 中的全部数据。ActivatedRouteSnapshot
还提供Observable
接口,用于读取/接收预期信息(路径参数、查询参数等)。所见即所得
=>所见即所得
When we implement the interface
CanActiavte
onResetPasswordGuard
, the functioncanActivate
takes 2 parameters, first of which isActivatedRouteSnapshot
...So one can easily read path parameters from it using...
Using an
ActivatedRouteSnapshot
one has access to the whole data in the URL.ActivatedRouteSnapshot
also providesObservable
interfaces for reading/receiving intended information(path params, query params etc...).WYSIWYG
=>WHAT YOU SHOW IS WHAT YOU GET