【问题标题】:Angular and RxJS - merge multiple HTTP requests, but emit the outer one instantlyAngular 和 RxJS - 合并多个 HTTP 请求,但立即发出外部请求
【发布时间】:2022-11-24 05:15:07
【问题描述】:

我很难理解 RxJS 运算符的某些方面。我这里有一些代码(在一个组件中):

extendedOrders$ = this.salesService.getOrders().pipe(
  switchMap(orders => {
    return forkJoin(orders.map(order => {
      return this.salesService.getOrderCustomers(order.id).pipe(
        map(customer => ({ ...order, customerName: customer.name })),
      );
    }));
  }),
);

是否有可能扩展它并在 forkJoin 中做不止一个调用?例如,另一个调用另一个 getById 服务并将响应合并到同一个对象中?

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    你可以为 forkJoin 方法做这样的事情:

    forkJoin({
    stream1: this.service.method1(),
    stream2:  this.service.method2()
    })
    .pipe(map(data => {
     const mergedObject = { ...data.stream1, ...data.stream2);
     return mergedObject;
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2019-12-25
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多