二次调用API实现QuantityCollected值累加功能需求求助
解决方案
要实现第二次调用API时QuantityCollected值累加第一次的结果,核心是在组件中维护一个持久的累计变量,同步每次API调用后的返回值,具体步骤如下:
1. 添加累计变量
在组件类中声明一个类级变量,用于存储累计的已收集数量(初始化为0):
// 组件类内部定义 totalCantidadRecogida = 0;
2. 修改API调用逻辑
调整API调用时传入的参数,将累计值与本次新增的收集量合并后传入,并在回调中同步更新累计变量:
anadirReserva() { // 计算本次要传入API的累计值:历史累计 + 本次新增的收集量 const cantidadAEnviar = this.totalCantidadRecogida + this.cantidadRecogida; this.api .gePickingOrderReserveAdd( this.token, this.userName, this.enterprise, this.warehouse, this.loadOrder, this.zone, this.article, this.tireName, this.zoneLocation, this.shelfLocation, this.moduleLocation, this.heightLocation, cantidadAEnviar, // 传入累加后的数值 this.internalOrder, this.id, this.quantity, this.errorFalta ) .subscribe((responsePickingOrderReserveAdd) => { const { result, value } = responsePickingOrderReserveAdd; if (result !== 0 || !value || !value.Rows || value.Rows.length === 0) { this.error = 'Error, no ha devuelto datos'; return; } // 从API返回结果中提取QuantityCollected(需根据实际返回字段名调整) const quantityCollected = value.Rows[0].QuantityCollected; // 更新本地累计变量,确保与API返回值一致 this.totalCantidadRecogida = quantityCollected; console.log(this.quantity, 'cantidad original'); console.log(this.totalCantidadRecogida, ' cantidad total recogida'); if (this.quantity === this.totalCantidadRecogida) { this.mostrarBotonConfirmacion = true; console.log(this.mostrarBotonConfirmacion); } // 可选:重置单次输入的收集量,方便下次输入 this.cantidadRecogida = 0; }); }
关键说明
totalCantidadRecogida作为组件的持久变量,会在组件生命周期内保留累计值,不会因方法调用重置。- 优先从API返回结果中同步累计值,避免本地计算与API业务逻辑不一致的问题。
- 若
this.cantidadRecogida是单次输入的收集量,调用后重置为0可提升用户体验。
内容的提问来源于stack exchange,提问作者Miriam
相关产品推荐
相关产品推荐

