在一个绑定对象中添加一个响应式属性:
例如:
var vm = new Vue({
data: {
userProfile: {
name: 'Anika'
}
}
})
vm.$set(vm.userProfile, 'age', 27)
有时你可能需要为已有对象赋予多个新属性,比如使用 Object.assign() 或 _.extend()。在这种情况下,你应该用两个对象的属性创建一个新的对象。
例如:
vm.userProfile = Object.assign({}, vm.userProfile, {
age: 27,
favoriteColor: 'Vue Green'
})
用watch监听绑定属性,通过设置深度监听属性来监听属性变化。
watch:{
xxx:{
handler(newVal,odlVal){
console.log(newVal,'为更新后的值');
console.log(odlVal,'为更新前的值');
},
deep:true
}
}