vue3 侦听器有哪些写法
Vue3 侦听器有下列写法:
- 监听单个数据变化:
watch: {
count: {
handler(newVal, oldVal) {
console.log('count changed:', newVal, oldVal)
},
immediate: true
}
}
- 监听多个数据变化:
watch: {
count(newVal, oldVal) {
console.log('count changed:', newVal, oldVal)
},
message(newVal, oldVal) {
console.log('message changed:', newVal, oldVal)
}
}
- 监听对象属性变化:
watch: {
userInfo: {
handler(newVal, oldVal) {
console.log('userInfo changed:', newVal, oldVal)
},
deep: true
}
}
- 监听嵌套对象属性变化:
watch: {
'userInfo.name': {
handler(newVal, oldVal) {
console.log('userInfo.name changed:', newVal, oldVal)
}
}
}
- 监听计算属性变化:
watch: {
fullName(newVal, oldVal) {
console.log('fullName changed:', newVal, oldVal)
}
}
原文地址: https://www.cveoy.top/t/topic/bilz 著作权归作者所有。请勿转载和采集!