【发布时间】:2021-08-28 16:21:50
【问题描述】:
我使用CanActivate 来保护一个页面,但它总是返回 false,因为我无法访问受保护的路由器。我尝试了很多方法,但未能成功解决问题。我是 Angular 新手,我一直在验证条件 if (data.hasPermission == true)。
谁能帮帮我?
auth.service.ts
//Checking user access
getUserAccess(name: string): Observable<any> {
return this.http.get(`api/${name}/useraccess`).pipe(
map(
(response: any) => {
return response;
},
),
);
};
checkUserPermission() {
this.getUserAccess(this.name).subscribe(data => {
this.hasaccess = false;
if (data.hasPermission == true) {
this.hasaccess = true;
} else {
this.hasaccess = false;
}
});
return this.hasaccess
}
isUserHasAccess(): boolean {
return this.hasaccess
}
auth.guard.ts
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.isUserHasAccess()) {
return true;
} else {
return false;
}
}
{"hasPermission":true}
【问题讨论】:
标签: angular angular-router canactivate