深度监听
watch: {
obj: {
// 数据变化时执行的逻辑代码
handler(newName, oldName) {
console.log(‘obj.a changed’);
},
// 开启深度监听
deep: true
}
}
普通监听
new Vue({
el: '#root',
data: {
firstName: '张',
lastName: '三',
fullName: ''
},
watch: {
firstName(newName, oldName) {
this.fullName = newName + ' ' + this.lastName;
}
}
})
监听某一个属性
watch: {
'person.name': {
handler(newName, oldName) {
console.log('person.name changed');
},
}
}