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

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

一、通过deep属性实现

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

  

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

二、通过computed做中介

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

  

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

  


返回目录

posted @ 2019-07-03 12:35  gitByLegend  阅读(6081)  评论(0编辑  收藏  举报