Categories
Vue Answers

How to get the old value when on change event with Vue.js?

Spread the love

Sometimes, we want to get the old value when on change event with Vue.js.

In this article, we’ll look at how to get the old value when on change event with Vue.js.

How to get old value when on change event with Vue.js?

To get the old value when on change event with Vue.js, we can get the value from the 2nd parameter of the watcher.

For instance, we write

<script>
//...
export default {
  watch: {
    clonedItems: {
      deep: true,
      handler(newVal, oldVal) {
        console.log(JSON.stringify(newVal));
        console.log(JSON.stringify(oldVal));
      },
    },
  },
  //...
};
</script>

to get the old value of the clonedItems reactive property from the oldVal parameter.

Conclusion

To get the old value when on change event with Vue.js, we can get the value from the 2nd parameter of the watcher.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *