Angular 2 canActivate async

回眸只為那壹抹淺笑 提交于 2019-12-11 05:47:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!