Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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
Javascript rxjs:使用管道返回值(映射)_Javascript_Angular_Rxjs_Reactivex - Fatal编程技术网

Javascript rxjs:使用管道返回值(映射)

Javascript rxjs:使用管道返回值(映射),javascript,angular,rxjs,reactivex,Javascript,Angular,Rxjs,Reactivex,使用管道和贴图时如何返回值,如本场景中所示: return this.service.someEvent().pipe( map(x => { return this.service.someValue; }) ) 但是,如果我这样使用它,则该值将永远不会返回,因为从未对事件进行过细分: const a = this.service.someEvent().pipe( map(x => { return this.service.someValue;

使用管道和贴图时如何返回值,如本场景中所示:

return this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)
但是,如果我这样使用它,则该值将永远不会返回,因为从未对事件进行过细分:

const a = this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)

return a.subscribe();
我得到一个错误(我知道这是错误的)

TS2322:类型'Subscription'不可分配给类型'boolean | Observable | Promise'。类型“Subscription”不可分配给类型“Promise”。类型“订阅”中缺少属性“then”。
所以我需要一种返回值的方法

更新: 更好的描述(我需要用最新版本的rxjs复制此代码):

canActivate(下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此.service.something().map(x=>{
if(x){
返回true;
}否则{
返回false;
}
})
}


需要使用管道内部的映射(据我所知)。

您只需从“canActivate”保护返回可观察对象本身即可:-

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().pipe(map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  }));
}
canActivate(下一步:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回这个.service.something().pipe(映射(x=>{
if(x){
返回true;
}否则{
返回false;
}
}));
}

只有在返回的可观察对象完成并且其发出的值为true之后,路由才会继续。因此,您实际上不必担心订阅可观察的角度路由器将自行处理它。

这看起来像是您试图调用
返回a.subscribe()
在一个方法中,其类型表示它应该返回
可观察的
。函数
subscribe()
返回
subscribe
实例
canActivate(next: ActivatedRouteSnapshot,
               state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  })
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().pipe(map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  }));
}