提交 c47c6c81 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

fix(watch): 修复 vue 版本升级后 watch 示例报错问题

上级 6d69b25d
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
<text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text> <text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text>
<text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text> <text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text>
<text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text> <text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch count trigger number:
{{ watchCountTriggerNum }}</text>
<text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result: <text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result:
{{ watchCountCleanupRes }}</text> {{ watchCountCleanupRes }}</text>
<button class="increment-btn uni-common-mb" @click="increment"> <button class="increment-btn uni-common-mb" @click="increment">
...@@ -17,6 +15,8 @@ ...@@ -17,6 +15,8 @@
stop watch count stop watch count
</button> </button>
<text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text> <text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch obj.str trigger number:
{{ watchObjStrTriggerNum }}</text>
<text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text> <text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text>
<text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text> <text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text>
<text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text> <text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text>
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
const watchCountRes = ref('') const watchCountRes = ref('')
const watchCountCleanupRes = ref('') const watchCountCleanupRes = ref('')
const watchCountTrackNum = ref(0) const watchCountTrackNum = ref(0)
const watchCountTriggerNum = ref(0)
const stopWatchCount = watchEffect((onCleanup : OnCleanup) => { const stopWatchCount = watchEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) { if (countRef.value !== null) {
...@@ -74,12 +73,6 @@ ...@@ -74,12 +73,6 @@
watchCountTrackNum.value++ watchCountTrackNum.value++
} }
}, },
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchCountTriggerNum.value++
}
}
}) })
const triggerStopWatchCount = () => stopWatchCount() const triggerStopWatchCount = () => stopWatchCount()
...@@ -102,6 +95,8 @@ ...@@ -102,6 +95,8 @@
const objStrRef = ref<UniTextElement | null>(null) const objStrRef = ref<UniTextElement | null>(null)
const watchObjStrRes = ref('') const watchObjStrRes = ref('')
const watchObjStrTriggerNum = ref(0)
watchEffect(() => { watchEffect(() => {
if (objStrRef.value !== null) { if (objStrRef.value !== null) {
// #ifdef APP // #ifdef APP
...@@ -116,6 +111,12 @@ ...@@ -116,6 +111,12 @@
}, { }, {
// 侦听器在组件渲染之前触发 // 侦听器在组件渲染之前触发
flush: 'pre', flush: 'pre',
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchObjStrTriggerNum.value++
}
}
}) })
const objBoolRef = ref<UniTextElement | null>(null) const objBoolRef = ref<UniTextElement | null>(null)
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
<text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text> <text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text>
<text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text> <text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text>
<text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text> <text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch count trigger number:
{{ watchCountTriggerNum }}</text>
<text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result: {{ watchCountCleanupRes }}</text> <text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result: {{ watchCountCleanupRes }}</text>
<button id="increment-btn" class="uni-common-mb" @click="increment"> <button id="increment-btn" class="uni-common-mb" @click="increment">
increment increment
...@@ -16,6 +14,8 @@ ...@@ -16,6 +14,8 @@
stop watch count stop watch count
</button> </button>
<text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text> <text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch obj.str trigger number:
{{ watchObjStrTriggerNum }}</text>
<text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text> <text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text>
<text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text> <text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text>
<text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text> <text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text>
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
const watchCountRes = ref('') const watchCountRes = ref('')
const watchCountCleanupRes = ref('') const watchCountCleanupRes = ref('')
const watchCountTrackNum = ref(0) const watchCountTrackNum = ref(0)
const watchCountTriggerNum = ref(0)
const stopWatchCount = watchPostEffect((onCleanup : OnCleanup) => { const stopWatchCount = watchPostEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) { if (countRef.value !== null) {
...@@ -70,12 +69,6 @@ ...@@ -70,12 +69,6 @@
if (event.type === 'get') { if (event.type === 'get') {
watchCountTrackNum.value++ watchCountTrackNum.value++
} }
},
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchCountTriggerNum.value++
}
} }
}, },
) )
...@@ -100,6 +93,8 @@ ...@@ -100,6 +93,8 @@
const objStrRef = ref<UniTextElement | null>(null) const objStrRef = ref<UniTextElement | null>(null)
const watchObjStrRes = ref('') const watchObjStrRes = ref('')
const watchObjStrTriggerNum = ref(0)
watchPostEffect(() => { watchPostEffect(() => {
if (objStrRef.value !== null) { if (objStrRef.value !== null) {
// #ifdef APP // #ifdef APP
...@@ -111,6 +106,13 @@ ...@@ -111,6 +106,13 @@
} else { } else {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ` watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: `
} }
},{
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchObjStrTriggerNum.value++
}
}
}) })
const watchObjArrRes = ref('') const watchObjArrRes = ref('')
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
<text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text> <text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text>
<text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text> <text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text>
<text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text> <text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch count trigger number:
{{ watchCountTriggerNum }}</text>
<text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result: <text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result:
{{ watchCountCleanupRes }}</text> {{ watchCountCleanupRes }}</text>
<button id="increment-btn" class="uni-common-mb" @click="increment"> <button id="increment-btn" class="uni-common-mb" @click="increment">
...@@ -17,6 +15,8 @@ ...@@ -17,6 +15,8 @@
stop watch count stop watch count
</button> </button>
<text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text> <text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch obj.str trigger number:
{{ watchObjStrTriggerNum }}</text>
<text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text> <text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text>
<text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text> <text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text>
<text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text> <text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text>
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
const watchCountRes = ref('') const watchCountRes = ref('')
const watchCountCleanupRes = ref('') const watchCountCleanupRes = ref('')
const watchCountTrackNum = ref(0) const watchCountTrackNum = ref(0)
const watchCountTriggerNum = ref(0)
const stopWatchCount = watchSyncEffect((onCleanup : OnCleanup) => { const stopWatchCount = watchSyncEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) { if (countRef.value !== null) {
...@@ -58,10 +57,10 @@ ...@@ -58,10 +57,10 @@
watchCountRes.value = `count: ${count.value}, count ref text: ${(countRef.value!.childNodes[0] as HTMLElement).innerText}` watchCountRes.value = `count: ${count.value}, count ref text: ${(countRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif // #endif
} else { } else {
watchCountRes.value = `count: ${ count.value }, count ref text: ` watchCountRes.value = `count: ${count.value}, count ref text: `
} }
const cancel = () => { const cancel = () => {
watchCountCleanupRes.value = `watch count cleanup: ${ count.value }` watchCountCleanupRes.value = `watch count cleanup: ${count.value}`
} }
onCleanup(cancel) onCleanup(cancel)
}, },
...@@ -71,12 +70,6 @@ ...@@ -71,12 +70,6 @@
if (event.type === 'get') { if (event.type === 'get') {
watchCountTrackNum.value++ watchCountTrackNum.value++
} }
},
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchCountTriggerNum.value++
}
} }
}, },
) )
...@@ -96,37 +89,46 @@ ...@@ -96,37 +89,46 @@
const watchObjRes = ref('') const watchObjRes = ref('')
watchSyncEffect(() => { watchSyncEffect(() => {
watchObjRes.value = `obj: ${ JSON.stringify(obj) }` watchObjRes.value = `obj: ${JSON.stringify(obj)}`
}) })
const objStrRef = ref<UniTextElement | null>(null) const objStrRef = ref<UniTextElement | null>(null)
const watchObjStrRes = ref('') const watchObjStrRes = ref('')
const watchObjStrTriggerNum = ref(0)
watchSyncEffect(() => { watchSyncEffect(() => {
if (objStrRef.value !== null) { if (objStrRef.value !== null) {
// #ifdef APP // #ifdef APP
watchObjStrRes.value = `str: ${ obj.str }, obj.str ref text: ${ objStrRef.value!.value }` watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${objStrRef.value!.value}`
// #endif // #endif
// #ifdef WEB // #ifdef WEB
watchObjStrRes.value = `str: ${ obj.str }, obj.str ref text: ${ (objStrRef.value!.childNodes[0] as HTMLElement).innerText }` watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${(objStrRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif // #endif
} else { } else {
watchObjStrRes.value = `str: ${ obj.str }, obj.str ref text: ` watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: `
}
}, {
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchObjStrTriggerNum.value++
}
} }
}) })
const watchObjArrRes = ref('') const watchObjArrRes = ref('')
watchSyncEffect(() => { watchSyncEffect(() => {
watchObjArrRes.value = `arr: ${ JSON.stringify(obj.arr) }` watchObjArrRes.value = `arr: ${JSON.stringify(obj.arr)}`
}) })
const watchCountAndObjNumRes = ref('') const watchCountAndObjNumRes = ref('')
watchSyncEffect(() => { watchSyncEffect(() => {
watchCountAndObjNumRes.value = `count: ${ count.value }, obj.num: ${ obj.num }` watchCountAndObjNumRes.value = `count: ${count.value}, obj.num: ${obj.num}`
}) })
const updateObj = () => { const updateObj = () => {
obj.num++ obj.num++
obj.str = `num: ${ obj.num }` obj.str = `num: ${obj.num}`
obj.bool = !obj.bool obj.bool = !obj.bool
obj.arr.push(obj.num) obj.arr.push(obj.num)
} }
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
<text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text> <text id="count" ref="countRef" class="uni-common-mb">count: {{ count }}</text>
<text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text> <text id="watch-count-res" class="uni-common-mb">watch count result: {{ watchCountRes }}</text>
<text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text> <text id="watch-count-track-num" class="uni-common-mb">watch count track number: {{ watchCountTrackNum }}</text>
<text id="watch-count-trigger-num" class="uni-common-mb">watch count trigger number:
{{ watchCountTriggerNum }}</text>
<text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result: <text id="watch-count-cleanup-res" class="uni-common-mb">watch count cleanup result:
{{ watchCountCleanupRes }}</text> {{ watchCountCleanupRes }}</text>
<button class="increment-btn uni-common-mb" @click="increment"> <button class="increment-btn uni-common-mb" @click="increment">
...@@ -17,6 +15,8 @@ ...@@ -17,6 +15,8 @@
stop watch count stop watch count
</button> </button>
<text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text> <text id="obj-str" ref="objStrRef" class="uni-common-mb">obj.str: {{ obj.str }}</text>
<text id="watch-obj-str-trigger-num" class="uni-common-mb">watch obj.str trigger number:
{{ watchObjStrTriggerNum }}</text>
<text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text> <text id="obj-num" class="uni-common-mb">obj.num: {{ obj.num }}</text>
<text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text> <text id="obj-bool" ref="objBoolRef" class="uni-common-mb">obj.bool: {{ obj.bool }}</text>
<text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text> <text id="obj-arr" ref="objArrRef" class="uni-common-mb">obj.arr: {{ JSON.stringify(obj.arr) }}</text>
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
const watchCountRes = ref('') const watchCountRes = ref('')
const watchCountCleanupRes = ref('') const watchCountCleanupRes = ref('')
const watchCountTrackNum = ref(0) const watchCountTrackNum = ref(0)
const watchCountTriggerNum = ref(0)
const stopWatchCount = watch(count, (count : number, prevCount : number, onCleanup : OnCleanup) => { const stopWatchCount = watch(count, (count : number, prevCount : number, onCleanup : OnCleanup) => {
// #ifdef APP // #ifdef APP
...@@ -64,19 +63,14 @@ ...@@ -64,19 +63,14 @@
}, { }, {
// 侦听器在响应式依赖改变时立即触发 // 侦听器在响应式依赖改变时立即触发
flush: 'sync', flush: 'sync',
// 响应属性或引用作为依赖项被跟踪时调用 // 响应属性或引用作为依赖项被跟踪时调用
// TODO: vue 3.4 开始, 数据更新,也会触发 onTrack, 待升级 vue 版本时,需要确认该变化是否合理及跟进 // TODO: vue 3.4 开始, 数据更新,也会触发 onTrack, 待升级 vue 版本时,需要确认该变化是否合理及跟进
onTrack(event : DebuggerEvent) { onTrack(event : DebuggerEvent) {
if (event.type === 'get') { if (event.type === 'get') {
watchCountTrackNum.value++ watchCountTrackNum.value++
} }
},
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchCountTriggerNum.value++
}
} }
// TODO: vue>3.4.15 开始 onTrack onTrigger 同时存在修改响应式数据时,会报错 Maximum call stack size exceeded
}) })
const triggerStopWatchCount = () => stopWatchCount() const triggerStopWatchCount = () => stopWatchCount()
...@@ -100,6 +94,7 @@ ...@@ -100,6 +94,7 @@
const objStrRef = ref<UniTextElement | null>(null) const objStrRef = ref<UniTextElement | null>(null)
const watchObjStrRes = ref('') const watchObjStrRes = ref('')
const watchObjStrTriggerNum = ref(0)
watch(() : string => obj.str, (str : string, prevStr : string) => { watch(() : string => obj.str, (str : string, prevStr : string) => {
// #ifdef APP // #ifdef APP
watchObjStrRes.value = `str: ${str}, prevStr: ${prevStr}, obj.str ref text (flush pre): ${objStrRef.value!.value}` watchObjStrRes.value = `str: ${str}, prevStr: ${prevStr}, obj.str ref text (flush pre): ${objStrRef.value!.value}`
...@@ -110,6 +105,12 @@ ...@@ -110,6 +105,12 @@
}, { }, {
// 侦听器在组件渲染之前触发 // 侦听器在组件渲染之前触发
flush: 'pre', flush: 'pre',
// 侦听器回调被依赖项的变更触发时调用
onTrigger(event : DebuggerEvent) {
if (event.type === 'set') {
watchObjStrTriggerNum.value++
}
}
}) })
const objBoolRef = ref<UniTextElement | null>(null) const objBoolRef = ref<UniTextElement | null>(null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册