vue.js $watch array of objects

后端 未结 4 1833
甜味超标
甜味超标 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:31

    You should pass an object instead of boolean as options, so:

    mounted: function () {
      this.$watch('things', function () {
        console.log('a thing changed')
      }, {deep:true})
    }
    

    Or you could set the watcher into the vue instance like this:

    new Vue({
      ...
      watch: {
        things: {
          handler: function (val, oldVal) {
            console.log('a thing changed')
          },
          deep: true
        }
      },
      ...
    })
    

    [demo]

提交回复
热议问题