S
S
sinneren2020-09-17 14:57:13
Vue.js
sinneren, 2020-09-17 14:57:13

How to track change in nested data object in vue2?

There is such an object in data:

...
{
   ...
    obj_lev_1: {
        obj_lev2: {
            need_watch: true,
        }
        obj_lev2_2: {
           need_watch: true,
        }
    },
    ...
}
...


I need to track changes (watch), in the properties need_watch, of each object.
If you try to follow obj_lev_1, then nothing will come of it. And the entry `obj_lev1.obj_lev2.need_watch: function (old, new) { ... }` is invalid. So how do you do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sinneren, 2020-09-17
@sinneren

watch: {
            obj_lev_1: {
                deep: true,
                handler(data) {
                    if (data.obj_lev2.need_watch) {
                        ...
                    } else if (data.obj_lev2_2.need_watch) {
                        ...
                    }
                },
            },
        },

found this way, but maybe there is a better way?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question