我必须在 angular 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;
我没有收到正确的回复。感谢任何线索..
试试这样:
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
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句