【问题标题】:RxJS: Subscribe to multiple independent observables and notify when all are doneRxJS:订阅多个独立的可观察对象并在所有完成时通知
【发布时间】:2022-10-06 21:58:42
【问题描述】:

在我的页面被渲染之前,我想从 5 个独立的不同 API 中获取数据(不需要等待第一个来从第二个获取数据),一旦它们都完成,我想要一个布尔变量要设置。像这样的东西

http.get(url1).subscribe(response => {
  // set response data 
  // & somehow notify that this call is complete?
});

http.get(url2).subscribe(response => {
  // set response data
  // & somehow notify that this call is complete?
});

...

如何使用 Angular 和 RxJS 实现这一点?

标签: angular rxjs


【解决方案1】:

只需使用 forkJoin

forkJoin([http.get(url1), http.get(url2)]).subscribe(dataAsArray => {
   // do what you need with the data
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 2017-04-13
    • 2018-07-28
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多