Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello-uvue
提交
c47c6c81
H
hello-uvue
项目概览
DCloud
/
hello-uvue
通知
350
Star
2
Fork
7
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello-uvue
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
c47c6c81
编写于
3月 21, 2024
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(watch): 修复 vue 版本升级后 watch 示例报错问题
上级
6d69b25d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
52 addition
and
46 deletion
+52
-46
pages/composition-api/reactivity/watch-effect/watch-effect.uvue
...composition-api/reactivity/watch-effect/watch-effect.uvue
+10
-9
pages/composition-api/reactivity/watch-post-effect/watch-post-effect.uvue
...n-api/reactivity/watch-post-effect/watch-post-effect.uvue
+11
-9
pages/composition-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
...n-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
+20
-18
pages/composition-api/reactivity/watch/watch.uvue
pages/composition-api/reactivity/watch/watch.uvue
+11
-10
未找到文件。
pages/composition-api/reactivity/watch-effect/watch-effect.uvue
浏览文件 @
c47c6c81
...
@@ -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)
...
...
pages/composition-api/reactivity/watch-post-effect/watch-post-effect.uvue
浏览文件 @
c47c6c81
...
@@ -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('')
...
...
pages/composition-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
浏览文件 @
c47c6c81
...
@@ -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)
}
}
...
...
pages/composition-api/reactivity/watch/watch.uvue
浏览文件 @
c47c6c81
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录