一只萌萌小番薯
2022-10-08 15:05:52
我想在另一個完成后執行一個 http get 請求。第二個請求的端點 URL 需要依賴于第一個請求。我嘗試在 Angular 中使用類似這樣的內容嵌套請求:this.http.get(endpoint1).subscribe( success => { this.http.get(endpoint2 + success).subscribe( anotherSuccess => console.log('hello stackoverflow!') ); });關于stackoverflow的第一個問題,如果我應該提供更多細節,請告訴我。
2 回答

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
在這里你可以找到如何做到這一點,你有多種選擇:
訂閱:
this.http.get('/api/people/1').subscribe(character => {
this.http.get(character.homeworld).subscribe(homeworld => {
character.homeworld = homeworld;
this.loadedCharacter = character;
});
});
與mergeMap
this.homeworld = this.http
.get('/api/people/1')
.pipe(mergeMap(character => this.http.get(character.homeworld)));
有一個帶有 forkJoin 的版本,但與 subscribe 和 mergeMap 相比并不明確。
添加回答
舉報
0/150
提交
取消