我有一个名为TransactionEntityService的服务,它是从EntityCollectionServiceBase派生出来的,用于名为Transaction的模型。
export class TransactionEntityService
extends EntityCollectionServiceBase<Transaction> {
我正在使用TransactionDataService来覆盖DefaultDataService的默认行为。
在AppModule中,TransactionDataService的注册方式如下
export class AppModule {
constructor(
private eds: EntityDefinitionService,
private entityDataService: EntityDataService,
private transactionsDataService: TransactionsDataService
) {
eds.registerMetadataMap(entityMetadata);
entityDataService.registerService('Transaction', transactionsDataService);
}
}
TransactionsDataService覆盖了getAll,如下所示。
export class TransactionsDataService extends DefaultDataService<Transaction> {
constructor(
http: HttpClient,
httpUrlGenerator: HttpUrlGenerator,
private notifyService: NotificationService
) {
super('Transaction', http, httpUrlGenerator);
}
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();
})
);
}
实体服务的“$entity”属性在调用api后返回正确的结果。我正在过滤该结果,以获取名为last6Month DepositCount$的可观察对象中的某些内容的计数。
this.last6MonthDepositCount$ = this.transactionsEntityService.entities$.pipe(
map((transactions) => {
const res = transactions.filter(
(transaction) =>
transaction.transactionType === TransactionType.Deposit
).length;
return res;
})//,
// tap((val) => this.depositCount = val)
);
在html中,我可以使用这个observable
{{ last6MonthDepositCount$ | async }}
它奏效了。
我应该怎么做才能在代码中的另一个变量中使用这个observable的值?
this.last6MonthDepositCount$.subscribe(x => this.dipositCount = x);
这种代码不起作用。我在dipositCount中得到0,它看起来像可观测值的初始值。