Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular2中并发获取多个HTTP资源_Http_Typescript_Angular - Fatal编程技术网

在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,这个问题解决了。回答得很好!这里也有详细的解释