防止来自 Pipe(takeUntil()) 的多个 API 调用

问题描述 投票:0回答:0

我有这样的方法:

async refreshTable(filters, clearFilters: boolean = true) {
    if (clearFilters) {
      this._dashService.resetFilters(filters);
    }

    if (this.productfilter.isPersistentFilter) {
      await this.setPersistentPaging();
      this.loadPresistentDisplayedColumns();

      this._dashService.getDashByStatus(this._status, this.sortBy, this.direction, this.pageIndex, this.pageSize)
      .pipe(takeUntil(this._unsubscribe)).subscribe(result => { this.setTableData(result, true); }); 
    } else {
      this._dashService.getDashByStatus(this._status)
      .pipe(takeUntil(this._unsubscribe)).subscribe(result => { this.setTableData(result, true); });
    }
  }

问题是上面的代码正在对后端进行多次 API 调用,我想阻止这种情况。

我试图通过完全删除管道(until())来解决这个问题,就像这样(效果很好):

async refreshTable(filters, clearFilters: boolean = true) {
    if (clearFilters) {
      this._dashService.resetFilters(filters);
    }

    if (this.productfilter.isPersistentFilter) {
      await this.setPersistentPaging();
      this.loadPresistentDisplayedColumns();

      const result = this._dashService.getDashByStatus(this._status, this.sortBy, this.direction, this.pageIndex, this.pageSize)
      this.setTableData(result, true); 
    } else {
      const result = this._dashService.getDashByStatus(this._status)
      this.setTableData(result, true); 

    }
  }

这是我的服务:

getDashByStatus(
    status: string,
    sortBy = '',
    sortOrder: 'asc' | 'desc' = 'desc',
    pageNumber = 1,
    pageSize = 10): Observable<any> {
    const params = {
      references: this.references,
      Ids: this.Ids,
      Date: this.Date,
    };
    if (!this.isActivated) return;
    return this.api.post(`api/dash/get-list/${status}`, params);
  }

这是一个好方法吗? 我应该如何改善这个..

angular rxjs observable
© www.soinside.com 2019 - 2024. All rights reserved.