vue3 watch监听 和报错处理
watch([props.count],(count, prevCount) => {console.log(count, 'count')})会出现以下报错1 A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types. ```是因为wat
·
watch(
[props.count],
(count, prevCount) => {
console.log(count, 'count')
}
)
会出现以下报错
1 A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types. ```
是因为watch只能监听响应式数据:ref定义的属性和reactive定义的对象,如果直接监听reactive定义对象中的属性是不允许的,除非使用函数转换一下;
```javascript
watch(
() => props.count,
(count, prevCount) => {
console.log(count, 'count')
}
)
这样就能监听到属性的某一项啦。
watch监听多个值
前面是监听数据的数组,后面的参数是两个数组,前面数组是变化后监听对象值的数组,后面是变化前监听对象值的数组
watch ([countObj, count], ([oneNewName, twoNewName], [oneOldName, twoOldName]) => {
console.log(oneNewName, oneOldName, twoNewName, twoOldName)
})
更多推荐
已为社区贡献1条内容
所有评论(0)