gpt4 book ai didi

angular - 等待两个 Observable 完成

转载 作者:行者123 更新时间:2023-12-04 22:00:16 24 4
gpt4 key购买 nike

我想在两个 Observable 返回值后调用一个方法。我做了一些搜索,似乎是 forkJoin是我想要的,但我无法让它工作。我知道这两个 Observable 都在返回值,因为我在组件的其他地方单独使用每个数据,所以很明显我做错了其他事情。

这是我尝试过的。我正在使用 rxjs v6.4。

forkJoin(
this.store.pipe(select(fromStore.getAppointmentsLoading)),
this.clientStore.pipe(select(fromClientStore.getClientsLoading)),
).subscribe(
([res1, res2]) => {
console.log('res1', res1);
console.log('res2', res2);
},
err => console.error(err),
);

没有任何内容记录到控制台,我也没有收到任何错误。同样,我传入的 Observable 肯定是返回值。

我是做错了什么,还是我完全使用了错误的方法 forkJoin ?

最佳答案

forkJoin当所有 observables 完成时(或当其中一个抛出错误时)发出,而不是在它们发出时发出。
您可以使用 combineLatest反而。
小心不是 'rxjs/operators' 导入实例运算符.这是由某些 IDE 自动导入功能引起的常见错误。在这种情况下,我们需要从 'rxjs' 导入的静态版本。 :

import {combineLatest} from 'rxjs';

combineLatest([
this.store.pipe(select(fromStore.getAppointmentsLoading)),
this.clientStore.pipe(select(fromClientStore.getClientsLoading)),
]).subscribe(
([res1, res2]) => {
console.log('res1', res1);
console.log('res2', res2);
},
err => console.error(err),
);

关于angular - 等待两个 Observable 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com