Javascript 我需要传递一个从api调用获得的id来调用另一个api调用
我需要传递一个从一个api调用获得的id来调用另一个api调用,不能让它工作! 有人能帮忙吗 第二个api将返回并调用图像,但它不起作用,并且在vs代码中变灰 如果有人能举个例子,我将不胜感激Javascript 我需要传递一个从api调用获得的id来调用另一个api调用,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我需要传递一个从一个api调用获得的id来调用另一个api调用,不能让它工作! 有人能帮忙吗 第二个api将返回并调用图像,但它不起作用,并且在vs代码中变灰 如果有人能举个例子,我将不胜感激 currentResult: any; getData(){ this.showLoading(); const headers = { "x-api-key":'', 'Content-Type': 'a
currentResult: any;
getData(){
this.showLoading();
const headers = {
"x-api-key":'',
'Content-Type': 'application/json',
"Access-Control-Allow-Origin":"*",
"x-requested-with": "xmlhttprequest",
/* 'Authorization': `Bearer ${this.sygicBearerToken}` */
}
return this.http.get(
this.ProxyUrl1+'https://api.sygictravelapi.com/1.2/en/places/list?parents=city:2&categories=discovering&limit=10', { headers: headers },
)
.pipe(tap (value => {
this.loadingController.dismiss();
console.log(value);
}))
this.http.get(
this.ProxyUrl1+'https://api.sygictravelapi.com/1.2/en/places/{id}/media',{ headers: headers},)
.pipe(tap (value => {
this.loadingController.dismiss();
console.log(value);
})
);
}
由于您有一个包含第一个this.http.get的return语句,因此return语句之后的任何内容都不会运行,因此它会变灰。第一个:第二个API调用变灰,因为它是在
return
调用之后写入的
为了使用第一次调用的结果调用其他API调用,
您可以使用RXJSswitchMap
这将帮助您获得一个接一个的连续请求
例如,exmaple:
get data() {
return this.http.get('http://firstAPI').pipe(
switchMap(id => this.http.get(`http://secondAPI/${id}`))
);
}
第一个API调用结果“id”被传递给第二个http调用
阅读更多: