从Observable.forkjoin获取json

时间:2017-08-27 16:20:51

标签: angular rxjs observable

Observable.forkJoin(
  this.ids.map(
    i => this.http.get('api_url' + i) //return a json for each url
      .map(res=> res.json())
  )).subscribe(res=> this.data = res.data)

它提示[ts] Property 'data' does not exist on type 'any[]'.所以如何从forkjoin响应中获取json?

1 个答案:

答案 0 :(得分:0)

ForkJoin将返回响应数组,您需要循环遍历数组以获取每个请求的数据。

    this.data = [];
Observable.forkJoin( this.ids.map( i => this.http.get('api_url' + i) //return a json for each url .map(res=> res.json()) )).subscribe(aRes=> {
    aRes.forEach((res) =>  this.data.push( res.data));
});