vue lodash element input @input 防抖

1、安装引入

cnpm install lodash --save
import _ from "lodash";

2、使用

2-1、价格修改调接口

<el-input
 v-model="scope.row.itemPrice"
  type="number"
  placeholder="请输入价格"
  @input="changePrice(scope.row)"
></el-input>
changePrice: _.debounce(async row => {
      row.itemCharge = Number(row.itemPrice) * Number(row.itemNumber);
      let params = {
        id: row.id,
        itemPrice: row.itemPrice,
        itemNumber: row.itemNumber,
        itemCharge: row.itemCharge
      };
      let res = await API["editPackageServiceLittleTypeById"](params);
      if (res.code != 1) return Message.error(res.message);
      Message.success(res.message);
    }, 500),

2-2、修改input

<el-input
  @input="changeTotalDiscountCharge"
   v-model.number="datalist.chargeSettle.totalDiscountCharge"
   :maxlength="20"
   type="number"
 ></el-input>
changeTotalDiscountCharge: _.debounce(function(e) {
      if (e < 0) {
        this.$message.error("优惠金额不能小于0");
        this.datalist.chargeSettle.totalDiscountCharge = 0;
        return;
      }
      if (e > Number(this.datalist.chargeSettleWay[0].payCharge)) {
        this.$message.error("优惠金额不能大于待付款金额");
        this.datalist.chargeSettle.totalDiscountCharge = 0;
        return;
      }

      this.datalist.chargeSettleWay[0].payCharge =
        Number(this.initTotal) - e - Number(this.initCheep);
    }, 500)

注意:在lodash的throttle方法中,可以直接使用function,而且额外的好处是无需重新指向this,在函数内部中,已经做了apply,所以这里的this指向的就是vue实例,如果用箭头函数,this为undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值