合并 combineLatest 和 switchMap 时未定义

undefined when combine combineLatest and switchMap

我正在尝试将两个 Observable 组合在一起,以便为调用接受两个值的服务方法提供很少的值。但是这里我几乎没有错误

  1. 定义可观察对象和主题。

     private openSortQuerySubject: BehaviorSubject<string> = new BehaviorSubject<string>(this.formatSortQuery(this.multiSortMeta));
     public openSortQuery: Observable<string> = this.openSortQuerySubject.asObservable();
    
     private closedSortQuerySubject: BehaviorSubject<string> = new BehaviorSubject<string>(this.formatSortQuery(this.multiSortMeta));
     public closedSortQuery = this.closedSortQuerySubject.asObservable();
    
  2. 合并观测值。

       const openQueries$ = combineLatest([this.openFilterQuery, this.openSortQuery]);
       const closedQueries$ = combineLatest([this.closedFilterQuery, this.closedSortQuery]);
    
  3. 在服务方法中使用合并值。

openQueries$
      .pipe(
        switchMap(([filter, sort]) => {
          this.alertService
            .listAlerts(filter, sort);
        }),
        tap(v => console.log(v))
      )
      .subscribe((openAlerts) => {
        this.openAlertsCount = openAlerts.length;
        this.openAlerts = this.parseAlerts(openAlerts);
      });

    closedQueries$
      .pipe(
        switchMap(([filter, sort]) => {
          this.alertService
            .listAlerts(filter, sort);
        })
      )
      .subscribe((closedAlerts) => {
        this.closedAlertsCount = closedAlerts.length;
        this.closedAlerts = this.parseAlerts(closedAlerts);
      });

你必须在 switchMap 回调中 return 一个 observable(或者 RxJS 知道如何变成一个 observable 的东西)。

不是这个:

switchMap(([filter, sort]) => {
  this.alertService.listAlerts(filter, sort);
})

但是这个(假设 listAlerts return 是一个可观察的、一个承诺或类似的):

switchMap(([filter, sort]) => {
  return this.alertService.listAlerts(filter, sort);
})