Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度8:等待订阅响应_Angular_Rxjs - Fatal编程技术网

Angular 角度8:等待订阅响应

Angular 角度8:等待订阅响应,angular,rxjs,Angular,Rxjs,当我从服务器请求一些数据时,我的身份验证程序出现了一些问题 我的路由选择取决于服务器的响应 如何等待下一步的服务器响应 这是我的代码: canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (!this._sessionService.user) { this._router.navigate(['login']); return false

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

这是我的代码:

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;
    })

}
试试这个

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;
}))

}

canActivate
返回可观测值,并使用pipe+map而不是
subscribe

警卫可以返回可观测值,这正是在授权导航的决定是异步的情况下。不要订阅。使用RxJS映射运算符将您的可观察对象转换为可观察对象,并返回该可观察对象。@JB Nize您能举个例子吗?谢谢@yashpatelyk。你们有关于rxjs操作员的课程吗?我没有课程,但我认为官方文档已经足够好了。它也有例子。