在以下代码片段中。首先,我从返回 Observable 的 GeoCode 服务中获取 lat lng。之后我进行 http 调用,再次返回一个 observable
getData(next?) {
return this.geoCode.getCurrentLocation().map(latlng => {
console.log(latlng);
var url = this.API_URL + "?location=" + latlng + "&radius=" + this.radius;
if (next != undefined) {
url = this.API_URL + "?location=" + latlng + "&radius=" + this.radius + "&next=" + next;
}
return this._http.get(url).map(res=>res.json());
});
};
上面的方法正在返回,Observable<Observable<any>>
但我想返回Observable<any>
您必须使用flatMap
代替 map :
getData(next?) {
return this.geoCode.getCurrentLocation().flatMap(latlng => { //HERE !
console.log(latlng);
let url = this.API_URL + "?location=" + latlng + "&radius=" + this.radius;
if (next != undefined) {
url = this.API_URL + "?location=" + latlng + "&radius=" + this.radius + "&next=" + next;
}
return this._http.get(url).flatMap(res=>res.json()); //AND HERE !
});
};
这里有一个很好的阅读:为什么我们需要使用 flatMap?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句