簡體   English   中英

將 3 個 async 合並為一個 observable

[英]Combine 3 async to one observable

我有 3 個可觀察值用於過濾 html 部分

這是 typescript 零件

   fiscalYear$ = this.store$.select(this.tableStoreService.getFiscalYear);
      isLoading$ = this.store$.select(this.tableStoreService.tableSelectors.getLoading);
isReportLoading$ = this.store$.select(isReportLoading);

所有這些值的返回類型都有Observable<boolean>

然后我像這樣使用它來過濾[disabled]中的html

<button class="btn btn-primary font-weight-700 font-size-200" [disabled]="(isReportLoading$ | async) || (fiscalYear$ | async) < 2022 || (isLoading$ | async) " (click)="clickExport()">
          {{ 'homepage.export' | translate }}
          <mat-icon class="btn-icon export-icon" svgIcon="export"></mat-icon>
        </button>

我試着這樣做

get isExportAvialable$() {
    return combineLatest(this.fiscalYear$, this.isLoading$, this.isReportLoading$, (fiscalYear, isLoading, isReportLoading) => isReportLoading || fiscalYear < 2022 || isLoading);
  }

fiscalYear < 2022顯示錯誤

我如何將它結合起來只使用一個異步?

你可以像這樣解決它:

fiscalYear$ = this.store$.select(this.tableStoreService.getFiscalYear);
isLoading$ = this.store$.select(this.tableStoreService.tableSelectors.getLoading);
isReportLoading$ = this.store$.select(isReportLoading);
isExportAvailable$ = combineLatest([
    this.fiscalYear$, 
    this.isLoading$, 
    this.isReportLoading$
]).pipe(map(([year, isLoading, isReportLoading]) => return year < 2022 && isLoading && isReportLoading));

然后只需在模板中使用 isExportAvailable$ 和異步 pipe 即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM