如何解决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 举报,一经查实,本站将立刻删除。