我想知道是否有多个请求:
if my $http request 1
开始吧,比方说$http request 1
结束并尝试打电话$http request 2
. 我的问题如何创建多个请求?
例如:打电话$http request 1
then $http request 2
.
据我了解,您正在尝试发出多个 http 请求,然后在所有这些请求结束后处理响应。例如,您可能需要从多个源加载数据,并延迟加载后逻辑,直到所有数据加载完毕。
如果是这种情况,您可以使用 ReactiveX Observables,因为它提供了一个名为forkJoin()
包装多个 Observables。
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class MultipleHttpService {
constructor(private http:Http) { }
// If any single request fails, the entire operation will result in an error state.
getData0AndData1() {
return Observable.forkJoin(
this.http.get('/app/data0.json').map((res:Response) => res.json()),
this.http.get('/app/data1.json').map((res:Response) => res.json())
);
}
}
然后你可以通过订阅该 observable 来获取所有数据:
// Code in your page ...
this.myMultipleHttpService.getData0AndData1()
.subscribe(
data => {
this.data0= data[0]
this.data1 = data[1]
},
err => console.error(err)
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)