【问题标题】:How to store each response from a loop of multiple API calls in angular如何以角度存储来自多个 API 调用循环的每个响应
【发布时间】:2023-01-19 04:17:54
【问题描述】:

我想在一个带有 ID 数组的循环中进行 API 调用,并将每个 API 调用的响应作为数组存储到一个变量中。

const myIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 19]

 forkJoin(
    myIds.map((i: number) => 
      this.http.get('apicallwith/${i}')
      .subscribe(res) => {
        console.log(res) // always getting the response of my first ID
      }
    )
  )

我根据我的理解尝试了 forkJoin,但不确定我哪里做错了

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    你必须使用combineLatest这里是例子

    import { combineLatest } from 'rxjs';
    const myIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 19]
    combineLatest(...(myIds.map(id => this.http.get('apicallwith/${id}'))))
    .subscribe((values)=>{
       console.log(values)
    })
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多