關(guān)于 vue 不能 watch 數(shù)組變化 和 對象變化的解決方案

博客地址:關(guān)于 vue 不能 watch 數(shù)組變化 和 對象變化的解決方案

vue 監(jiān)聽數(shù)組和對象的變化

vue 監(jiān)聽數(shù)組

vue 實際上可以監(jiān)聽數(shù)組變化,比如:

data () {
  return {
    watchArr: [],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr = [1, 2, 3];
  }, 1000);
},

再如使用 splice(0, 2, 3) 從數(shù)組下標 0 刪除兩個元素,并在下標 0 插入一個元素 3:

data () {
  return {
    watchArr: [1, 2, 3],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr.splice(0, 2, 3);
  }, 1000);
},

push 數(shù)組也能夠監(jiān)聽到


vue 無法監(jiān)聽數(shù)組變化的情況

但是,數(shù)組在下面兩種情況無法監(jiān)聽:

  1. 利用索引直接設置一個數(shù)組項時,例如:arr[indexOfItem] = newValue;
  2. 修改數(shù)組的長度時,例如:arr.length = newLength;


舉例無法監(jiān)聽數(shù)組變化的情況

  1. 利用索引直接修改數(shù)組值
data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr[0].name = 'xiaoyue';
  }, 1000);
},
  1. 修改數(shù)組的長度
    長度大于原數(shù)組就將后續(xù)元素設置為 undefined
    長度小于原數(shù)組就將多余元素截掉
data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr.length = 5;
  }, 1000);
},

上面的 watchArr 變成
[圖片上傳失敗...(image-f5ff59-1538822570758)]

vue 無法監(jiān)聽數(shù)組變化的解決方案

  1. this.$set(arr, index, newVal);
data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    this.$set(this.watchArr, 0, {name: 'xiaoyue'});
  }, 1000);
},
  1. 使用數(shù)組 splice 方法可以監(jiān)聽,例子上面有

  2. 使用臨時變量直接賦值的方式,原理與直接賦值數(shù)組一樣

data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('監(jiān)聽:' + newVal);
},
created () {
  setTimeout(() => {
    let temp = [...this.watchArr];
    temp[0] = {
      name: 'xiaoyue',
    };
    this.watchArr = temp;
  }, 1000);
},

vue 監(jiān)聽對象

vue 可以監(jiān)聽直接賦值的對象

this.watchObj = {name: 'popo'};

vue 不能監(jiān)聽對象屬性的添加、修改、刪除

vue 監(jiān)聽對象的解決方法

  1. 使用 this.$set(object, key, value)
  2. 使用深度監(jiān)聽 deep: true,只能監(jiān)聽原有屬性的變化,不能監(jiān)聽增加的屬性
mounted () {
  // 這里使用深度監(jiān)聽 blog 對象的屬性變化,會觸發(fā) getCatalog 方法
  this.$watch('blog', this.getCatalog, {
    deep: true,
  });
},
  1. 使用 this.set(obj, key, val) 來新增屬性(vue 無法監(jiān)聽 this.set 修改原有屬性)
this.$set(this.watchObj, 'age', 124);

delete this.watchObj['name'](vue 無法監(jiān)聽 delete 關(guān)鍵字來刪除對象屬性)

  1. 使用 Object.assign 方法,直接賦值的原理監(jiān)聽(最推薦的方法)
this.watchObj = Object.assign({}, this.watchObj, {
  name: 'xiaoyue',
  age: 15,
});

博客地址:關(guān)于 vue 不能 watch 數(shù)組變化 和 對象變化的解決方案

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • # 傳智播客vue 學習## 1. 什么是 Vue.js* Vue 開發(fā)手機 APP 需要借助于 Weex* Vu...
    再見天才閱讀 3,800評論 0 6
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時...
    歐辰_OSR閱讀 30,252評論 8 265
  • 概要 64學時 3.5學分 章節(jié)安排 電子商務網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,852評論 0 3
  • 自從小孩上幼兒園開始,我便開始了每天接送他上下學的生活。突然閑散下來的生活讓我有點無所適從,剛開始的時候覺...
    東鑫秀閱讀 172評論 0 1
  • 早上媽媽帶雪梅去公園玩。到了公園,雪梅看見公園里的花五彩繽紛,開得很好看,就隨手摘了幾朵。但是她卻沒有看見旁邊有一...
    銘宇記錄閱讀 757評論 0 0

友情鏈接更多精彩內(nèi)容