【问题标题】:vuejs2 change method not working currentlyvuejs2更改方法当前不起作用
【发布时间】:2020-01-12 23:14:45
【问题描述】:

我有使用 vuejs2 的项目 这是html代码

<input @keyup='check_item_offer(arrayresult.id, arrayresult.unit_id, arrayresult.qty, key, 16)' v-model='arrayresult.qty' />

这是 vuejs2 方法中的 check_item_offer 代码

check_item_offer:function(item_id ,unit_id, quantity, key, tax) {
  this.taxss = tax;
  $.ajax({
    type:'POST',
    url: path + 'check_item_offer',
    data: {
      item_id: item_id,
      unit_id: unit_id,
      quantity: quantity
    },
    success:(data) => {
      console.log(data);
      if(data != '') {
        $.ajax({
          type:'POST',
          url: path + 'get_item_data',
          data: {
            item_id: item_id,
            unit_id: unit_id
          },
          success:(datas) => {
            data['item_sale_offer_detale_price'] = data['item_sale_offer_detale_price'] * (1 + this.taxss / 100);
            datas = datas * (1 + this.taxss / 100);
            var qty = this.arrayresults[key].qty;
            var old_price = datas;
            var offer_quantity = data['item_sale_offer_detale_quantity'];
            var remain = this.arrayresults[key].qty % data['item_sale_offer_detale_quantity'];
            var new_price = data['item_sale_offer_detale_price'];
            var item_with_new_price_quantity = qty - remain;
            var price_with_offers = (item_with_new_price_quantity / offer_quantity) * new_price;
            var price_without_offers = old_price * remain;
            var total_price = (price_with_offers + price_without_offers) / qty;
            this.arrayresults[key].item_smallest_unit_selling_price = total_price;
            this.arrayresults[key].items_quantity_selling_price = total_price;
          }
        });
      }
    }
  });
}

一切正常

如果我使用 keyup 更改 arrayresult.qty 它将正常工作 但是如果我使用 ajax 调用任何其他方式更改arrayresult.qty keyup 它不会运行check_item_offer,直到我进入输入并按任意键

即使我更改了 arrayresult.qty 甚至 ajax 调用,我如何运行 check_item_offer
谢谢

【问题讨论】:

  • 注释this.taxss = tax;之后的所有代码并再次检查?还要检查 console.log(item_id,unit_id,quantity,key,tax) 中item_id,unit_id,quantity,key,tax 的值并共享?
  • 变化发生在 this.taxss = tax;在ajax调用中
  • aah..thisreference...this里面ajax成功回调的通病和你想象的完全不一样。

标签: vue.js vuejs2 vue-router


【解决方案1】:

如果您希望触发函数,check_item_offer,设置为 keyup 事件以更改 v-model,arrayresult.qty,以编程方式设置值,ajax 调用,那么你需要像这样为 v-model 变量设置一个 watch

watch:{
    'arrayresult.qty'(newVal, oldVal){
        this.check_item_offer(itemId, unitId, newVal, key, 16);
    }
}

更多关于 vuejs 的信息watch

【讨论】:

  • 它对我不起作用,但如果我像这样更换手表:{ arrayresults(newVal, oldVal){ alert("Asdfasdf"); //this.check_item_offer(itemId, unitId, newVal, key, 16); } },它的工作
  • 我需要观察 arrayresult.qty 的任何变化
  • 你能检查一下这支笔吗?一个用例已被模拟。 codepen.io/zapping-the-selector/pen/GRKGXYr?editors=1011
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多