handler:监听数组或对象的属性时用到的方法

deep:深度监听,为了发现对象内部值的变化,可以在选项参数中指定 deep:true 。注意监听数组的变动不需要这么做。

1,watch监听普通变量:

data() {
return {
  frontPoints: 0
}
},
watch: {
frontPoints(newValue, oldValue) {
  console.log(newValue)
}
}

2,watch监听数组:

data() {
 return {
   winChips: new Array(11).fill(0)
 }
},
watch: {
  winChips: {
 handler(newValue, oldValue) {
   for (let i = 0; i < newValue.length; i++) {
  if (oldValue[i] != newValue[i]) {
      console.log(newValue)        
     }
  }
   }, 
  }
}

3,watch监听对象:

data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    }
 }
},
watch: {
  bet: {
    handler(newValue, oldValue) {
      console.log(newValue)    
},
    deep: true
  }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐