我有一个 Angular 应用程序,在其中我设置了 ngrx 数据,但我注意到每次调用实体服务时
IE
this.purchaseOrderEntityService.update(updatedPurchaseOrder);
它似乎运行了两次并两次访问 http 端点以达到这种效果,似乎无法弄清楚为什么
这是电话之一
if (this.editItem) {
this.subs.sink = this.purchaseService
.updatePurchaseOrderItem(newPoItem)
.subscribe((res: IPurchaseOrderItems) => {
this.itemsService.updateItem(this.purchaseOrderItems, (u) => u.id === res.id, res);
this.settingsService.addinfo("Updated");
this.closeEditor(sender);
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
});
} else {
newPoItem.purchaseOrderId = this.purchaseOrder?.id;
this.subs.sink = this.purchaseService
.addPurchaseOrderItem(newPoItem)
.subscribe((res: IPurchaseOrderItems) => {
this.itemsService.addItemToStart(this.purchaseOrderItems, res);
this.settingsService.addinfo("Added");
this.closeEditor(sender);
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
});
}
当我们删除一个项目时也调用它
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
完整的更新功能:
updatePurchaseOrderPrices(): Observable<IPurchaseOrder> {
return this.purchaseOrderEntityService.entities$.pipe(
map((po) => po.find((p) => p.id == this.purchaseOrder.id)),
tap((order) => {
const totalNetPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.netAmount, 0); // total amount of the purchase order items including net price
const totalItemPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.totalAmount, 0); // total amount of the purchase order including tax
const totalTaxAmount = totalItemPrice - totalNetPrice;
const updPo: Partial<IPurchaseOrder> = {
id: order.id,
netAmount: totalNetPrice, // should be net item price
taxAmount: totalTaxAmount,
totalAmount: totalItemPrice, //* 0.2,
};
const updatePurchaseOrder: IPurchaseOrder = {
...order,
...updPo,
};
console.log(updatePurchaseOrder);
this.purchaseOrderEntityService.update(updatePurchaseOrder);
}),
first()
);
}
被覆盖的更新函数
update(details: Update<IPurchaseOrder>): Observable<IPurchaseOrder> {
return this.http
.put<IPurchaseOrder>(
`${this.baseUrl}/purchaseorder/poorder/${details.id}`,
{
...details.changes,
}
)
.pipe(
map((res: IPurchaseOrder) => {
return res;
})
);
}
任何想法可能会发生什么?