我有基于專案的 angular 12。
我需要根據收集令牌的引數來保護路由:
export const authenticationRoutes = [
{
path: 'reset-password/token/:token',
component: ResetPasswordShellComponent,
canActivate: [ResetPasswordGuard]
}
];
ResetPasswordGuard 是一個呼叫 Web 服務的守衛,它應該發送作為路徑一部分的令牌值:
path: 'reset-password/token/:token'
我的問題是有什么方法可以將令牌值從路徑傳遞到警衛(ResetPasswordGuard),以便將其發送到 Web 服務?
uj5u.com熱心網友回復:
當我們在 上實作介面CanActiavte時ResetPasswordGuard,函式canActivate有 2 個引數,第一個是ActivatedRouteSnapshot...
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
// your logic goes here
console.log('[ResetPasswordGuard]', next.params); // <-- This should print {token: 'somevalue'}
....
....
}
因此,可以使用...輕松地從中讀取路徑引數...
const tokenInPath = next.params.token || ''
使用ActivatedRouteSnapshotone 可以訪問 URL 中的全部資料。ActivatedRouteSnapshot還提供Observable用于讀取/接收預期資訊的介面(路徑引數、查詢引數等...)。
WYSIWYG=>WHAT YOU SHOW IS WHAT YOU GET
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442525.html
