簡(jiǎn)單變量的監(jiān)聽
{
data() {
return {
username: "dennis",
userid: 125
}
},
watch: {
username(value) {
console.log("username 被修改了", value);
},
userid: {
handler: function(newValue, oldValue) {
console.log("username 被修改了", newValue, oldValue);
}
}
}
}
對(duì)象的監(jiān)聽
- 通過deep可以監(jiān)聽對(duì)象的屬性对嚼;
- 通過immediate可以控制是否需要第一次渲染;
{
data() {
return {
user: {
username: "dennis",
userid: 125
}
}
},
watch: {
user: {
handler: function(newVal, oldVal) { ... },
deep: true
},
"user.username": {
handler: function(newVal, oldVal) { ... },
immediate: true
}
}
}