Vuejs Watch Nested Properties

Jan 30, 2018

If you just need to watch one of the nested property.

export default {  name: 'TestNestedWatch',  data: function () {    return {      state: {        isEdit: false,        isSending: false      },    }  },  watch: {    'state.isEdit': function (newValue, oldValue) {      console.log(newValue)    }  }  }    

If you want to avoid quoted property, you can use a computed hack.

export default {  name: 'TestNestedWatch',  data: function () {    return {      state: {        isEdit: false,        isSending: false      },    }  },  computed: {    stateIsEdit() {      return this.state.isEdit    }  },  watch: {    stateIsEdit: function (newValue, oldValue) {      console.log(newValue)    }  }  }  

If you need to watch the entire object.

export default {  name: 'TestNestedWatch',  data: function () {    return {      state: {        isEdit: false,        isSending: false      },    }  },  watch: {    state: {      handler(val) {        console.log(val)      },      deep: true    }  }  }    

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.