0

我通过驱动我的服务来创建服务

EntityCollectionServiceBase<Transaction>

我的实体称为事务。

我正在像这样覆盖 getAll 方法

  getAll(): Observable<Transaction[]> {
return this.http
  .get<ApiResponse>('https://localhost:xxxx/transaction/GetLastSixMonth')
  .pipe(
    tap((data) => {
      this.notifyService.showSuccess(data.message, 'Sucess');
    }),
    map((res) => res.result),
    catchError((err) => {
      this.notifyService.showError(
        'Error While Six Month Transactions',
        'Error'
      );
      return of();
    })
  );
}

我在适当的地方将此 getAll 称为。

所以在我想使用它的组件中,我正在使用这个服务的“entities $”属性,就像这样

this.last6MonthWithdrawCount$ = this.transactionsDataService.entities$.pipe(
  map((transactions) => {
    const res = transactions.filter(
      (transaction) =>
        transaction.transactionType === TransactionType.Withdraw
    ).length;
    return res;
  })
);

因此,当我使用 asyn 运算符在 html 中使用它时,这个变量“this.last6MonthWithdrawCount$”可以正常工作。

{{ last6MonthWithdrawCount$  | async }}

但是当我想通过订阅它来读取它在代码中的值时

this.last6MonthWithdrawCount$.subscribe(x => this.withdrawCount = x);

我没有得到适当的价值。我得到 0 。看起来它给了我流中的第一个价值。

如何获得最后一个值? 更新 或者只是我如何从代码中的 this.last6MonthWithdrawCount$ 获取值。实际上这个值应该被传递给图形数据。我有显示值的图表,而且我必须在图表的数据数组中传递相同的值。

在此处输入图像描述

这个值 45(与值 10 相同的东西)来自上面提到的实体 $.pipe 代码,在 html 中我正在做一个简单的 | 如上所述的异步,它可以工作。

我必须在我的 chartJS 图表的数据中传递相同的 2 个值。

  this.myChartData.data.datasets[0].data = [this.depositCount, this.withdrawCount];

如何从那个 observable 中得到这个值?与在 html 中工作的相同。

4

0 回答 0