在Angular2中并发获取多个HTTP资源
我可以处理来自在Angular2中并发获取多个HTTP资源,http,typescript,angular,Http,Typescript,Angular,我可以处理来自http的单个可观察结果。使用以下代码获取: http.get('/customers/1') .map((res: Response) => res.json()) .subscribe(customer => this.customer = customer); 现在我有了一个资源ID列表,比如var list:number[]=[1,4,7]我希望能够发送对所有资源的请求,并将所有已解析的项分配给我的数组,如customers=>
http的单个可观察结果。使用以下代码获取:
http.get('/customers/1')
.map((res: Response) => res.json())
.subscribe(customer => this.customer = customer);
现在我有了一个资源ID列表,比如var list:number[]=[1,4,7]
我希望能够发送对所有资源的请求,并将所有已解析的项分配给我的数组,如customers=>this.customers=customers
可以这样做
首次导入可观察对象和forkJoin:
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
还是全部导入
import {Observable} from 'rxjs/Rx';
现在使用forkJoin
连接所有可观察对象:
// a set of customer IDs was given to retrieve
var ids:number[] = [1, 4, 7];
// map them into a array of observables and forkJoin
Observable.forkJoin(
ids.map(
i => this.http.get('/customers/' + i)
.map(res => res.json())
))
.subscribe(customers => this.customers = customers);
检查谢谢@EricMartinez,这个问题解决了。回答得很好!这里也有详细的解释