Angular 正在从url获取id,其中param位于路由6的中间位置
我使用Angular 6和route guard保护编辑路由,该路由应:Angular 正在从url获取id,其中param位于路由6的中间位置,angular,Angular,我使用Angular 6和route guard保护编辑路由,该路由应: 检查以确保有已登录的用户(从存储器中的jwt令牌获取用户对象)。完成 验证url参数中的id是否与令牌中用户的id匹配 我的路线是这样的/members/:id/edit 我已经有一个名为doesUserIdInUrlMatchUserIdInToken的函数,它将返回一个布尔值 我的auth guard canActivate函数如下: export class UserEditAuthGuard implements
/members/:id/edit
我已经有一个名为doesUserIdInUrlMatchUserIdInToken
的函数,它将返回一个布尔值
我的auth guard canActivate函数如下:
export class UserEditAuthGuard implements CanActivate {
constructor(
private auth: AuthService,
private activatedR: ActivatedRoute,
private router: Router,
private alertify: AlertifyService,
private userService: UserService
) {}
canActivate(): boolean {
if (this.auth.loggedIn()) {
console.log(this.activatedR.params["id"]);
console.log(this.activatedR.snapshot.paramMap.get("id"));
return true;
}
this.alertify.error("You shall not pass!!!");
this.router.navigate(["/home"]);
return false;
}
// this.userService.doesUserIdInUrlMatchUserIdInToken();
}
这两个控制台日志都返回
null
和undefined
使用router.url
然后将其拆分以获取id的简单方法
ID: any = null;
constructor(router: Router) {
console.log(router.url); // This will print the current url
let URL = this.router.url;
let URL_AS_LIST = URL.split('/');
this.ID = URL_AS_LIST[1];
}