vue.js $watch array of objects

后端 未结 4 1935
甜味超标
甜味超标 2021-02-01 13:58
mounted: function() {
  this.$watch(\'things\', function(){console.log(\'a thing changed\')}, true);
}

things is an array of objects

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 14:27

    If someone needs to get an item that was changed inside the array, please, check it:

    JSFiddle Example

    The post example code:

    new Vue({
      ...
      watch: {
        things: {
          handler: function (val, oldVal) {
            var vm = this;
            val.filter( function( p, idx ) {
                return Object.keys(p).some( function( prop ) {
                    var diff = p[prop] !== vm.clonethings[idx][prop];
                    if(diff) {
                        p.changed = true;                        
                    }
                })
            });
          },
          deep: true
        }
      },
      ...
    })
    

提交回复
热议问题