[英]How to nest http calls in angular
嗨,我需要在 Angular9 中嵌套 3 個調用。 在哪里:
a.我怎樣才能以更好的方式編寫代碼?
b.我可以使用 RXJS 嗎?
c.如何與 RXJS 中的錯誤塊一起嵌套?
例子:
this.http.get('/api/people/1').subscribe(character => {
this.http.get('/api/people/character ').subscribe(homeworld => {
this.http.get('/api/people/character/homeworld ').subscribe(finalResponse=> {
console.log(finalResponse);
},
error =>{
console.log(error);
});
},
error =>{
console.log(error);
});
},
error =>{
console.log(error);
});
您將需要一個higher-order mapping operator
才能進行一次訂閱,例如switchMap
。
const getPeople = id => this.http.get(`/api/people/${id}`);
const getCharacter = character => this.http.get(`/api/people/${character}`);
const getCharacterAgain = character => this.http.get(`/api/people/character/${character}`);
getPeople(1).pipe(
switchMap(getCharacter),
switchMap(getCharacterAgain),
).subscribe(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.