因此,我有一个服务,其中包含我需要执行的三个功能。
我使用forkJoin
是因为我想在收到所有人的响应后采取进一步的行动!
其中之一需要接收一个参数。
getDevices(id: string): Observable<IEquipmentRow[]> {
const url = `${apiUrl}/${id}`;
return this.http.get<IGetDevicesResponse>(url)
.pipe(
map(res => {
return res.data;
})
);
}
private regions$ = this.getRegions();
private devices$ = this.getDevices();
public equipmentPreparationData$ = forkJoin({
regions: this.regions$,
devices: this.devices$
});
实现这一点的最佳方法是什么?也许使用RxJS
Subject
/BehaviorSubject
?
那RxJS
switchMap
呢,我们可以在这里使用它吗?
我是 RxJS
新手,所以要温柔:)
最佳答案
尝试:
// Change your response type
public equipmentPreparationData$(deviceID: string): Observable<any> {
return forkJoin({
regions: this.regions$,
devices: this.getDevices(deviceID)
});
private getDevices(id: string): Observable<IEquipmentRow[]> {
const url = `${apiUrl}/${id}`;
return this.http.get<IGetDevicesResponse>(url)
.pipe(
map(res => {
return res.data;
})
);
}
private regions$ = this.getRegions();
这样,您就可以使用带有参数的函数并通过 getDevices
方法传递
关于angular - 使用 RxJS forkJoin 时传递参数的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72358562/