等到两个Observable完成

问题描述 投票:0回答:1

我想在两个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),
);

没有任何东西登录到控制台,我没有收到任何错误。再次,我传入的Observables肯定是返回值。

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

angular rxjs ngrx fork-join
1个回答
3
投票

forkJoin在所有可观察物完成时发射,而不是在它们发射时发射。

你可以用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),
);
© www.soinside.com 2019 - 2024. All rights reserved.