Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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 如何避免ngrx观测值的回调地狱_Angular_Observable_Ngrx_Angular2 Observables - Fatal编程技术网

Angular 如何避免ngrx观测值的回调地狱

Angular 如何避免ngrx观测值的回调地狱,angular,observable,ngrx,angular2-observables,Angular,Observable,Ngrx,Angular2 Observables,我需要从redux store获取数据以进行另一个api调用,并且需要将响应保存到store。在这种情况下,我们如何避免callbackhell 下面是订阅observable的代码片段 贮藏 回叫地狱 如何避免上述回调。在承诺的世界里,我们可以很容易地把承诺连成链条。由于可观察对象是不可链接的,我们如何避免嵌套回调,以及编写上述代码的最佳方法是什么。想知道如何应用最好的rxjs运算符 非常感谢您的帮助。您希望利用rxjs提供的许多运营商。例如,您可以按如下方式重新编写上述代码: this.us

我需要从redux store获取数据以进行另一个api调用,并且需要将响应保存到store。在这种情况下,我们如何避免
callback
hell

下面是订阅observable的代码片段

贮藏

回叫地狱

如何避免上述回调。在承诺的世界里,我们可以很容易地把承诺连成链条。由于
可观察对象
是不可链接的,我们如何避免嵌套回调,以及编写上述代码的最佳方法是什么。想知道如何应用最好的rxjs运算符


非常感谢您的帮助。

您希望利用rxjs提供的许多运营商。例如,您可以按如下方式重新编写上述代码:

this.user$
.flatMap((user: any) => this.abcService.getData(user.id))
.subscribe((data: any) => {
    this.data = data;
    this.dataStore$.dispatch(abcAction(data));    
});

您可以将这些不同的运算符视为您的
承诺
then()
s,但功能更强大,因为它们可以做得更多。

可能的副本是否有关于所有rxjs运算符的示例的好文档?@Wishnu当然有。我觉得这一页很有帮助:
this.user$.subscribe((user: any) => {
  this.abcService.getData(user.id).subscribe((data:any) => {
    this.data = data;
    this.dataStore$.dispatch(abcAction(data));
  }
}
this.user$
.flatMap((user: any) => this.abcService.getData(user.id))
.subscribe((data: any) => {
    this.data = data;
    this.dataStore$.dispatch(abcAction(data));    
});