Angular 8:等待订阅响应

时间:2019-07-07 13:07:55

标签: angular rxjs

当我从服务器请求一些数据时,我的身份验证保护出现问题 而我的路由选择取决于服务器的响应 如何等待下一步的服务器响应?

这是我的代码:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (!this._sessionService.user) {
        this._router.navigate(['login']);
        return false;
    }

    this._userServiceProxy.get(this._sessionService.userId).subscribe((user: UserDto) => {
        if (user["result"].shouldChangePassword) {
            this._router.navigate([this.routeToChangePassword()]);
        }
        if ((!route.data || !route.data["permission"])) {
            return true;
        }

        if ((this._permissionChecker.isGranted(route.data["permission"]))) {
            return true;
        }
        this._router.navigate([this.selectBestRoute()]);
        return false;
    })

}

1 个答案:

答案 0 :(得分:1)

尝试一下

  | x `elem` ["swap"] =   swap_first_two_elements Just(s)

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (!this._sessionService.user) { this._router.navigate(['login']); return false; } return this._userServiceProxy.get(this._sessionService.userId).pipe(map((user: UserDto) => { if (user["result"].shouldChangePassword) { this._router.navigate([this.routeToChangePassword()]); } if ((!route.data || !route.data["permission"])) { return true; } if ((this._permissionChecker.isGranted(route.data["permission"]))) { return true; } this._router.navigate([this.selectBestRoute()]); return false; })) } 返回了可观察对象,并使用pipe + map代替了canActivate