问题
I have a service which checks does user has permissions to open route.
@Injectable()
export class GuardService implements CanActivate{
user: User;
constructor(private _userService: UserService) {
}
canActivate(): any{
return this._userService.getUser().subscribe(user => {
if(user.user_type == 'admin'){
return true;
}
return false;
});
}
But the problem is that it never passes. The problem is in this observable but I don't know how to handle it. How can I achieve this? Thanks in advance
回答1:
Return an Observable instead of a Subscription by changing subscribe to map
canActivate(): any{
return this._userService.getUser().map(user => {
if(user.user_type == 'admin'){
return true;
}
return false;
}); // <<== added
}
来源:https://stackoverflow.com/questions/41377014/angular-2-canactivate-async