我必须在角度2中发出三个HTTP请求,并且需要在应用程序上缓存响应对象。
以下实施的任何线索:
this._Data = this.http.get('http://www.example.com/one')
.map((res: Response) => res.json())
.flatMap((one: any) => {
return Observable.forkJoin(
Observable.of(one),
this.http.get('http://www.example.com/two').map((res: Response) => res.json()),
this.http.get('http://www.example.com/three').map((res: Response) => res.json()),
)
.do((data: any[]) => {
const one = data[0];
const two = data[1];
const three = data[2];
return one;
});
});
console.log(this._Data);
return this._Data;
我没有收到适当的回复。感谢任何线索..
答案 0 :(得分:0)
尝试这样:
this.http.get('http://www.example.com/one')
.flatMap((one: Response) => {
console.log('one Response', one);
return this.http.get('http://www.example.com/two')
})
.flatMap((two: Response) => {
console.log('two Response', two);
return this.http.get('http://www.example.com/three')
})
.subscribe((three: Response) => {
console.log('three Response', three);
})
如果您想要加速回复,请使用Observable.forkJoin
Observable.forkJoin(
this.http.get('http://www.example.com/one'),
this.http.get('http://www.example.com/two'),
this.http.get('http://www.example.com/three')
).subscribe((responses) => {
// responses[0] -> response of one
// responses[1] -> response of two
// responses[2] -> response of three
})