angular – 组合多个Observable
发布时间:2020-12-17 17:44:31 所属栏目:安全 来源:网络整理
导读:我有这两个可观测量: this.areasService.getSelectAreas({}) .subscribe((result) = this.areasSelect = result.json().data.areas);this.stationsService.getSelectStations({}) .subscribe((result) = this.stationsSelect = result.json().data.stations
|
我有这两个可观测量:
this.areasService.getSelectAreas({})
.subscribe((result) => this.areasSelect = result.json().data.areas);
this.stationsService.getSelectStations({})
.subscribe((result) => this.stationsSelect = result.json().data.stations);
填充这两个变量(异步)this.areasSelect& this.stationsSelect. 我还有一个第三个observable,我需要继续这些值: this.transportsService.getTransport().subscribe((res) => {
console.log(this.areasSelect)
console.log(this.stationsSelect)
})
我如何组合这3个可观察量? 解决方法
你可以使用forkJoin:
import { forkJoin } from 'rxjs/observable/forkJoin';
let areaSelect$= this.areasService.getSelectAreas({})
.map(res => res.json().data.areas)
.do(val => this.areaSelect = val);
let stationSelect$= this.stationsService.getSelectStations({})
.map(res => res.json().data.stations)
.do(val => this.stationSelect = val);
forkJoin(areaSelect$,stationSelect$)
.mergeMap(data=>{
//data is an array with 2 positions which contain the results of the 2 requests. data[0] is the vale of this.areaSelect for example
return this.transportsService.getTransport(data[0],data[1]);
}).subscrite(res => {
// res is the response value of getTransport
})
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
