前言
在学习Vue3的时候,在项目实战中,需要用到watch监听props内属性的值的变化 但是却出现了无响应的现象
虽然让他可以响应只需要对watch的监听对象做一点小小的修改,但是我们还是需要研究一下具体为什么某些做法无法传值
...
props: {
testData: {
type: Object,
default: () => {}
}
}
......
setup(props) {
watch(
() => props.testData,
(newValue, oldValue) => {
console.log(newValue, 'tttttnewValue', oldValue, 'tttttoldValue')
}
)
watch(
props.testData,
(newValue, oldValue) => {
console.log(newValue, 'tttttnewValue', oldValue, 'tttttoldValue')
}
)
}
问题分析
为了验证问题, 我进行了如下尝试
<template>
<my-form
:testData="test"
></my-form>
</template>
setup() {
const test = ref({})
const test1 = ref({})
watch(test, (newValue, oldValue) => {
let data = () => test
console.log(
isProxy(test),
isRef(test),
isProxy(test?.value),
isProxy(data()),
isRef(data()),
isReactive(test),
'test'
)
})
watch(test1, (newValue, oldValue) => {
let data = () => test1
console.log(
isProxy(test1),
isRef(test1),
isProxy(test1?.value),
isProxy(data()),
isRef(data()),
isReactive(test1),
'test1'
)
})
const getInfo = () => {
test.value = { ...formData.value }
test1.value = { ...formData.value }
}
const handleConfirmClick = () => {
getInfo()
}
return { test }
}
....
props: {
testData: {
type: Object,
default: () => {}
}
}
...
setup(props) {
watch(
() => props.testData,
(newValue, oldValue) => {
let data = () => props.testData
console.log(
isProxy(props.testData),
isRef(props.testData),
isProxy(props.testData.value),
isProxy(data()),
isRef(data()),
isReactive(props.testData),
'testData'
)
console.log(newValue, 'tttttnewValue', oldValue, 'tttttoldValue')
}
)
}
打印出来的结果如下: 可以看出,在经过prop,父传子操作之后,test对象发生了一定量的改变 test对象本身,在它所在的父组件中,依旧是一个ref对象,但是通过prop传值到子组件中后,获取test对象内值得对象却变成了一个reactive对象
然后就更加疑惑了,于是做出了下面的尝试
watch(props.testData, (newValue, oldValue) => {
let data = () => props.testData
console.log(
isProxy(props.testData),
isRef(props.testData),
isProxy(props.testData.value),
isProxy(data()),
isRef(data()),
isReactive(props.testData),
isReactive(props.testData.value),
'testData'
)
console.log(newValue, 'tttttnewValue', oldValue, 'tttttoldValue')
})
....
const getInfo = () => {
test.value.test = '11111'
}
结果监听到具体的打印了
于是乎开始了查看Vue3源码中watch的具体实现
let getter: () => any
let forceTrigger = false
if (isRef(source)) {
getter = () => {
console.log("getter重新执行了");
return (source as Ref).value;
}
forceTrigger = !!(source as Ref)._shallow
} else if (isReactive(source)) {
getter = () => source
deep = true
} else if (isArray(source)) {
getter = () =>
source.map(s => {
if (isRef(s)) {
return s.value
} else if (isReactive(s)) {
return traverse(s)
} else if (isFunction(s)) {
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
])
} else {
__DEV__ && warnInvalidSource(s)
}
})
} else if (isFunction(source)) {
if (cb) {
getter = () =>
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
instance && (instance.proxy as any)
])
} else {
getter = () => {
if (instance && instance.isUnmounted) {
return
}
if (cleanup) {
cleanup()
}
return callWithAsyncErrorHandling(
source,
instance,
ErrorCodes.WATCH_CALLBACK,
[onInvalidate]
)
}
}
} else {
getter = NOOP
__DEV__ && warnInvalidSource(source)
}
然后我们就可以得出结论了
结论
const test = ({})
watch(props.testData, (newValue, oldValue) => {})
....
const changeInfo = () => {
test.value.name = '1'
}
const changeInfo = () => {
test.value = { ...item.value }
}
于是
const changeInfo = () => {
test.value = { ...item.value }
}
watch(() => props.testData, (newValue, oldValue) => {})
|