Skip to content Skip to sidebar Skip to footer

Vue.js Watching Deep Properties

I'm trying to watch properties on a vue.js object, but i'm not getting the result that i want, my code is the following: var vueTable = new Vue({ el: '#vue-table', data: {

Solution 1:

The documentation covers this here.

Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion.

By starting with an empty object and setting v-model to filters.name, you end up adding a property dynamically. The best approach in this case would be to initialize the property in the data. It doesn't have to have a value.

data: {
    filters: { name:null },
}

Solution 2:

You can use something with the looks of this

this.$set(this.filters, 'name', "")

Using $set (as described in https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats) the observable will be correctly added

Solution 3:

You can put your filter object initialization in data property into the createdVue lifecycle hook.

var vueTable = newVue({
    el: '#vue-table',
    // If you attach filters at this point in time// the watcher will work normally.created: function () {
        this.$data.filters = {
            filter_one: null,
            filter_two: null
        };
    },
    data: {
        filters: null,
    },
    watch: {
        filters: {
            handler: function (newValue) {
                // Here you will get the entire filter object // if some property changes.console.dir(newValue);
            },
            deep: true
        }
    }
}

Post a Comment for "Vue.js Watching Deep Properties"