我正在使用回呼來組合兩個可觀察物件:
this._containerService.portlist(this.locationDetails.LocationType)
.subscribe(res=>{
this.portId = res.filter(port=>port.portCode ==result.locationAddress.portCode)[0].id;
this._portAddressService.getPortAddress(this.portId,this.locationDetails.LocationType).subscribe(res=>
{
this.portDetails = res.records[0];
}}
這不是一種有效的方法。有什么方法可以使用運算子來完成第一個 observable ,修改其回應,然后呼叫另一個 api。
uj5u.com熱心網友回復:
您可以使用“switchMap”運算子。
this._containerService.portlist(this.locationDetails.LocationType)
.pipe(
switchMap(res => {
this.portId = res.filter(port=>port.portCode ==result.locationAddress.portCode)[0].id;
return this._portAddressService.getPortAddress(this.portId,this.locationDetails.LocationType
}))
.subscribe(res => this.portDetails = res.records[0]);
uj5u.com熱心網友回復:
添加里奧魯多的答案,你可以走得更遠
this._containerService
.portlist(this.locationDetails.LocationType)
.pipe(
map(res => res.find(({ portCode }) => portCode === result.locationAddress.portCode),
pluck('id'),
switchMap(id => this._portAddressService.getPortAddress(id, this.locationDetails.LocationType),
map(({ records }) => records[0]),
tap(record => this.portDetails = record)
).subscribe(() => {
console.log('Port details set');
});
非常具有陳述性,使閱讀變得非常輕松
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495090.html
上一篇:Angular-如何在Angular界面中表示JSON登錄回應
下一篇:型別“節點”缺少以下屬性
