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)
    }
  },

  


返回目錄

相關文章
相關標籤/搜索