1

TEMPLATE :

<input type="number" min="0" class="form-control" @change="get_values($event)" v-model="value" required />

STATE :

data(){
  return {
    value: ""
  }
}

METHOD :

get_values (event) {
   var value = event.target.value
}

I get the current value but i didn't find how to get the old value when i add +1 in my input

Any ideas ?

LucasLaurens
  • 236
  • 1
  • 3
  • 14

4 Answers4

3

If you just need the old value whenever the input is updated, use a watcher, you don't need to listen to events:

export default {
    data() {
        return {
            value: ""
        }
    },
    watch: {
        value(newValue, oldValue) {
            console.log("You now have the new value: ", newValue, "and the old value: ", oldValue);
        }
    }
}
gabriel.hayes
  • 2,267
  • 12
  • 15
2

You can implement a v-model with a set() get() methods. This will allow a lot of flexibility in how the values are set. It will allowing you to set the data to something other than what was entered based on conditions:

var app = new Vue({
  el: '#app',
  data(){
    return {
      value: ""
    }
  },
  computed:{
    values: {
      get(){return this.value},
      set(v){
        console.log('old:', this.value)
        // loop values -- only allow 0-9
        this.value = v < 10 ? v : 0
        console.log('new:', this.value)
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="number" min="0" class="form-control"  v-model="values" required />
 </div>
Mark
  • 90,562
  • 7
  • 108
  • 148
1

You can try it with a watcher:

data(){
  return {
     value: ""
  }
},
watch: {
   value:function(newValue, oldValue){
      console.log(newValue)
      console.log(oldValue)
   }
}

its important that the watch method has the same name as your value defined in the data properties.

bill.gates
  • 14,145
  • 3
  • 19
  • 47
1

You can also use getter and setter:

data(){

    return {
      value_: 2,
      get value() {
        return this.value_
      },
      set value(val){
        console.log(/*new*/val, /*old*/this.value_)
        this.value_ = val
      }
    }
  },
Estradiaz
  • 3,483
  • 1
  • 11
  • 22