rxjs zip 两个 switchMap?
rxjs zip two switchMap?
我已经定义了一个行为主题:
measurementSearchChange$ = new BehaviorSubject('');
this.measurementSearchChange$
.asObservable()
.pipe(debounceTime(500))
.pipe(
switchMap((keyword: string) =>
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
)
)
)
.subscribe((data: any) => {
this.measurementOptions = data;
this.isLoading = false;
});
当有动作时,会这样做:
this.measurementSearchChange$.next(keyword);
它现在运行良好,但我想添加一个 switchMap 并将它们压缩,以便我可以订阅两个不同的数据,例如:
this.measurementSearchChange$
.asObservable()
.pipe(debounceTime(500))
.pipe(
switchMap((keyword: string) =>
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
)
// another
this.warningService.getInfluxdbQuery2(
this.selectedMonitorOption,
'measurement2',
{ search_name: keyword }
)
)
)
.subscribe((data1: any, data2: any) => {
this.measurementOptions = data;
this.isLoading = false;
});
那怎么办呢?感谢任何帮助
如果您的查询发出单个结果然后完成,您可以使用 forkJoin
,如下所示:
import { forkJoin } from 'rxjs';
/* ... */
this.measurementSearchChange$
.asObservable()
.pipe(
debounceTime(500),
switchMap((keyword: string) => forkJoin(
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
),
this.warningService.getInfluxdbQuery2(
this.selectedMonitorOption,
'measurement2',
{ search_name: keyword }
)
))
)
.subscribe(([data1, data2]: [any, any]) => {
this.measurementOptions = data;
this.isLoading = false;
});
如果他们发出多个结果,请使用 combineLatest
而不是 forkJoin
。
我不会使用 zip
,除非查询可以发出多个结果,并且可以保证每次一个查询发出一个结果时,另一个查询也会发出一个结果。
我已经定义了一个行为主题:
measurementSearchChange$ = new BehaviorSubject('');
this.measurementSearchChange$
.asObservable()
.pipe(debounceTime(500))
.pipe(
switchMap((keyword: string) =>
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
)
)
)
.subscribe((data: any) => {
this.measurementOptions = data;
this.isLoading = false;
});
当有动作时,会这样做:
this.measurementSearchChange$.next(keyword);
它现在运行良好,但我想添加一个 switchMap 并将它们压缩,以便我可以订阅两个不同的数据,例如:
this.measurementSearchChange$
.asObservable()
.pipe(debounceTime(500))
.pipe(
switchMap((keyword: string) =>
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
)
// another
this.warningService.getInfluxdbQuery2(
this.selectedMonitorOption,
'measurement2',
{ search_name: keyword }
)
)
)
.subscribe((data1: any, data2: any) => {
this.measurementOptions = data;
this.isLoading = false;
});
那怎么办呢?感谢任何帮助
如果您的查询发出单个结果然后完成,您可以使用 forkJoin
,如下所示:
import { forkJoin } from 'rxjs';
/* ... */
this.measurementSearchChange$
.asObservable()
.pipe(
debounceTime(500),
switchMap((keyword: string) => forkJoin(
this.warningService.getInfluxdbQuery(
this.selectedMonitorOption,
'measurement',
{ search_name: keyword }
),
this.warningService.getInfluxdbQuery2(
this.selectedMonitorOption,
'measurement2',
{ search_name: keyword }
)
))
)
.subscribe(([data1, data2]: [any, any]) => {
this.measurementOptions = data;
this.isLoading = false;
});
如果他们发出多个结果,请使用 combineLatest
而不是 forkJoin
。
我不会使用 zip
,除非查询可以发出多个结果,并且可以保证每次一个查询发出一个结果时,另一个查询也会发出一个结果。