vue可以通過watch監聽data內數據的變化。通常寫法是:
data: {
a: 100
},
watch: {
a(newval, oldVal) {
// 做點什么。。。
console.log(newval, oldVal)
}
}
vue監聽整個對象,如下:
- deep: true 深度監測
data: {
return {
msg: {
name: 'hahah',
color: 'red'
}
}
}
watch: {
msg: {
handler(newValue, oldValue) {
// 做點什么。。。
console.log(newValue)
},
deep: true
}
如果監聽對象內的某一具體屬性,可以通過computed做中間層來實現:
computed: {
name() {
return this.msg.name
}
},
watch:{
name(newValue, oldValue) {
// 做點什么。。。
console.log(newValue, oldValue)
}
}
