如何结合两种方法来计算每个类别的项目?

How to combine two methods for counting items per category?

我有 angular 8 个申请。

我有两种方法可以显示每个类别的项目数。来自后端的项目所以这是两个类别:

view.html:

  <mat-tab>
          <ng-template mat-tab-label>
            <mat-icon class="goals">grade</mat-icon>
            <span i18n>Goals</span>{{ dossierItemsCountString(itemTypes.Goal) }}
            <a [routerLink]="['../', dossier.id, 'item', 'new', itemTypes.Goal]"
              ><mat-icon class="add_box">add</mat-icon>
            </a>
          </ng-template>
          <ng-container *ngTemplateOutlet="itemList; context: { itemType: itemTypes.Goal }"></ng-container>
        </mat-tab>
        <mat-tab>
          <ng-template mat-tab-label>
            <mat-icon class="action-steps">list</mat-icon>
            <span i18n>Action steps</span>{{ dossierItemsCountString(itemTypes.ActionStep) }}
            <a [routerLink]="['../', dossier.id, 'item', 'new', itemTypes.ActionStep]"
              ><mat-icon class="add_box">add</mat-icon>
            </a>
          </ng-template>
          <ng-container *ngTemplateOutlet="itemList; context: { itemType: itemTypes.ActionStep }"></ng-container>
        </mat-tab>


这是负责显示每个类别的项目的两个函数:


  dossierItemsCountBy(itemType: DossierItemTypeDto) {
    return this.typeSearchMatches[itemType.toString()] || { total: 0, matches: 0 };
  }

  dossierItemsCountString(itemType: DossierItemTypeDto) {
    const count = this.dossierItemsCountBy(itemType);

    if (this.hasSearchQuery) {
      return `(${count.matches}/${count.total})`;
    } else {
      return `(${count.total})`;
    }
  }

不过我觉得这两个方法或许可以写成一个方法。或者你可以用它做一个烟斗?

所以我的问题是最好的方法是什么?以及如何做到这一点?

谢谢

我不知道如何回答问题"what is the best approach?"。

无论如何像这样修改 dossierItemsCountString() 应该有效:

const count = this.typeSearchMatches[itemType.toString()] || { total: 0, matches: 0 };