Angular (角度2/4/5/6)两个(或多个)内部订阅方法和一个外部订阅方法
我试图弄清楚如何用一个外部订阅方法调用两个内部订阅方法。总的来说,我想做3个api调用,但是,2个api调用取决于1个api调用的结果Angular (角度2/4/5/6)两个(或多个)内部订阅方法和一个外部订阅方法,angular,http,subscribe,switchmap,Angular,Http,Subscribe,Switchmap,我试图弄清楚如何用一个外部订阅方法调用两个内部订阅方法。总的来说,我想做3个api调用,但是,2个api调用取决于1个api调用的结果 this.subscription = this.quoteService //1st api call .get(this.quoteid) .pipe( switchMap((q: TradeinQuote) => { const quote = q["quote"]; //execute cod
this.subscription = this.quoteService //1st api call
.get(this.quoteid)
.pipe(
switchMap((q: TradeinQuote) => {
const quote = q["quote"];
//execute code using quote
this.price = quote["price"];
this.retrieveDeviceid = quote["deviceid"];
return this.faultService.get(this.retrieveDeviceid); //2nd api call
}),
).subscribe(
(faultGroup: FaultGroup[]) => {
//execute logic for 2nd api call
});
);
因此,在我从第一个api调用中获得this.retrieveDeviceid之后,我想再调用两个api,一个在faultService上(如图所示),另一个在deviceService上
E.g. this.deviceService.get(this.retrieveDeviceid)
目前显示的代码只处理1个内部订阅和1个外部订阅。为了做另一个内部订阅,我必须使用mergeMap或forkJoin吗?我该怎么做?感谢您的指导 使用
siwtchMap
和forkJoin
:
this.subscription=this.quoteService.get(this.quoteid).pipe(
开关映射(响应=>forkJoin(
(回应),
this.faultService.get(response.quote.deviceid),
这个.deviceService.get(response.quote.deviceid),
))
).subscribe(([res1,res2,res3])=>{…});
forkJoin
组合冷观测结果(即不再发出值的观测结果)
编辑
要管理单个呼叫的某些逻辑,您可以使用点击:
this.subscription=this.quoteService.get(this.quoteid).pipe(
轻触(response=>{/*使用第一个HTTP调用响应*/}执行一些代码),
开关映射(响应=>forkJoin(
(回应),
this.faultService.get(response.quote.deviceid),
这个.deviceService.get(response.quote.deviceid),
))
).subscribe(([res1,res2,res3])=>{…});
hey@trichetriche感谢您的帮助!如果我要为quoteService执行一些逻辑,该怎么办?(我已经更新了我的问题,上面写着“使用代码执行代码”)@iblehz我已经用tap
操作符更新了我的答案