[javascript] Vue.js - How to properly watch for nested data

I'm trying to understand how to properly watch for some prop variation. I have a parent component (.vue files) that receive data from an ajax call, put the data inside an object and use it to render some child component through a v-for directive, below a simplification of my implementation:

        <player v-for="(item, key, index) in players"

... then inside <script> tag:

     return {
         players: {}
        let self = this;
        this.$http.get('../serv/config/player.php').then((response) => {
            let pls = response.body;
            for (let p in pls) {
                self.$set(self.players, p, pls[p]);

item objects are like this:

   prop: value,
   someOtherProp: {
       nestedProp: nestedValue,
       myArray: [{type: "a", num: 1},{type: "b" num: 6} ...]

Now, inside my child "player" component I'm trying to watch for any Item's property variation and I use:

        //to work with changes in "myArray"
        //to work with changes in prop

It works but it seems a bit tricky to me and I was wondering if this is the right way to do it. My goal is to perform some action every time prop changes or myArray gets new elements or some variation inside existing ones. Any suggestion will be appreciated.

This question is related to javascript vue.js vue-component vue-resource

The answer is

You can use a deep watcher for that:

watch: {
  item: {
       // do stuff
     deep: true

This will now detect any changes to the objects in the item array and additions to the array itself (when used with Vue.set). Here's a JSFiddle: http://jsfiddle.net/je2rw3rs/


If you don't want to watch for every change on the top level object, and just want a less awkward syntax for watching nested objects directly, you can simply watch a computed instead:

var vm = new Vue({
  el: '#app',
  computed: {
    foo() {
      return this.item.foo;
  watch: {
    foo() {
      console.log('Foo Changed!');
  data: {
    item: {
      foo: 'foo'

Here's the JSFiddle: http://jsfiddle.net/oa07r5fw/

Similar questions with javascript tag:

Similar questions with vue.js tag:

Similar questions with vue-component tag:

Similar questions with vue-resource tag: