[英]How to nest http calls in angular
Hi I need to nest 3 calls in Angular9.嗨,我需要在 Angular9 中嵌套 3 个调用。 where:在哪里:
a.How can i write the code in a better way? a.我怎样才能以更好的方式编写代码?
b.Can i use RXJS? b.我可以使用 RXJS 吗?
c.How to nest along with error block in RXJS? c.如何与 RXJS 中的错误块一起嵌套?
example:例子:
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);
});
You will need a higher-order mapping operator
to be able to make only once subscription, for example, a switchMap
.您将需要一个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.