vue data中的对象的属性如何使用watch监听

  在写项目的时候遇到了一个问题,就是须要动态监听data中一个对象的属性的变化。遇到了许多坑,在此过程当中也发现了两种解决方案。javascript

1、经过deep属性实现

  data() {
    return {
      parent:{
        child:1
      }
    };
  },

  

  watch:{
    'parent.child':{
      deep:true,
      handler: function(newV, oldV) {
        console.log(newV);
      }
    }
  }

2、经过computed作中介html

  computed:{
    newChild(){
      return this.parent.child;
    }
  }

  

  watch:{
    newChild(newV,oldV){
      alert(newV)
    }
  },

  


返回目录

相关文章
相关标签/搜索