微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rxjs - 在 combinelatest 之后如何调用两个 httpService?

如何解决Rxjs - 在 combinelatest 之后如何调用两个 httpService?

我不知道这是否是更好的解决方案,但我需要一个函数订阅两个 ngrx 选择器并将其用作两个 http 服务的参数,我使用了 combineLatest:

  combineLatest([this.selectedCompany$,this.account$]).subscribe(res => {
  this.idCompany = res[0]!.id;
  this.account = res[1];
  this.getDashboardCardService(this.account!.id.toString(),this.idCompany.toString(),'time');
  this.getavailableSpaceService(this.idCompany).subscribe(res => {
      this.space = res;
  })

}).unsubscribe();

我使用 combineLatest 订阅两个 ngrx 选择器,并且在订阅中我需要使用这些值来执行两个 http 调用 getDashboardCard 和 available。它有效,但我不喜欢它......这样做的更好解决方案是什么?

解决方法

简而言之,您应该在 .pipe( 之后使用 combineLatest 并在该 pipe 中使用例如 switchMap 才能调用其他服务。

,

也许你可以用它来让你朝着正确的方向前进?

这里我使用了一个高阶可观察操作符 (concatMap);

combineLatest([this.selectedCompany$,this.account$]).pipe(
  concatMap(([company,account]) => forkJoin([
    this.getDashboardCardService(
      account!.id.toString(),company!.id.toString(),'time'
    ),this.getavailableSpaceService(company!.id)
  ]))
).subscribe(([res1,res2]) => {
  this.space = res2;
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?