Пожалуйста, помогите с угловыми щитками, у меня есть следующий угловой щиток:
export class RoleGuard implements CanActivate {
private role: any;
constructor(private router: Router, private accountService: AccountService)
{}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
this.accountService.getUserRole().subscribe(res => this.role = res);
if (this.role === 'admin') {
return true;
}
return false;
}
}
в сервисе:
getUserRole(): Observable<Response> {
const options = this.headers();
return this.http.get(`${environment.ApiUrl}/Roles/getRole`,options)
.map(res => res.json())
.catch(res => Observable.throw(res.json()));
}
Я пытаюсь подписаться на функцию getUserRole()
, затем присваиваю ответ this.role
, но этого не происходит, роль всегда undefined
. когда я делаю ...subscribe(res => console.log(res))
, я вижу данные ответа.