Categories
Vue Answers

How to watch a deeply nested object with Vue.js?

Spread the love

Sometimes, we want to watch a deeply nested object with Vue.js.

In this article, we’ll look at how to watch a deeply nested object with Vue.js.

How to watch a deeply nested object with Vue.js?

To watch a deeply nested object with Vue.js, we can use the property path as the key for the watcher method.

For instance, we write

<script>
export default {
  //...
  watch: {
    "block.checkbox.active"() {
      this.block.someotherprop.changeme = 5;
    },
  },
  //...
};
</script>

to watch the block.checkbox.active reactive property for changes.

When it changes, we run

this.block.someOtherProp = 5;

to set this.block.someOtherProp to 5.

Conclusion

To watch a deeply nested object with Vue.js, we can use the property path as the key for the watcher method.

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 *