Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello-uvue
提交
8d968cfd
H
hello-uvue
项目概览
DCloud
/
hello-uvue
通知
349
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看板
提交
8d968cfd
编写于
1月 19, 2024
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(composition api): 优化 watch 相关示例及测试
上级
4d404eb0
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
108 addition
and
132 deletion
+108
-132
pages/composition-api/reactivity/watch-effect/watch-effect.test.js
...position-api/reactivity/watch-effect/watch-effect.test.js
+19
-62
pages/composition-api/reactivity/watch-effect/watch-effect.uvue
...composition-api/reactivity/watch-effect/watch-effect.uvue
+21
-6
pages/composition-api/reactivity/watch-post-effect/watch-post-effect.uvue
...n-api/reactivity/watch-post-effect/watch-post-effect.uvue
+14
-4
pages/composition-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
...n-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
+21
-11
pages/composition-api/reactivity/watch/watch.test.js
pages/composition-api/reactivity/watch/watch.test.js
+11
-42
pages/composition-api/reactivity/watch/watch.uvue
pages/composition-api/reactivity/watch/watch.uvue
+22
-7
未找到文件。
pages/composition-api/reactivity/watch-effect/watch-effect.test.js
浏览文件 @
8d968cfd
...
...
@@ -12,14 +12,8 @@ describe('watchEffect', () => {
// watch
const
watchCountRes
=
await
page
.
$
(
'
#watch-count-res
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 0, count ref text (flush sync): count: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 0, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 0, count ref text (flush sync): count: 0
'
)
// track
const
watchCountTrackNum
=
await
page
.
$
(
'
#watch-count-track-num
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 3
'
)
...
...
@@ -38,14 +32,8 @@ describe('watchEffect', () => {
await
incrementBtn
.
tap
()
expect
(
await
count
.
text
()).
toBe
(
'
count: 1
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, count ref text (flush sync): count: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, count ref text (flush sync): count: 0
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 3
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 2
'
)
expect
(
await
watchCountCleanupRes
.
text
()).
toBe
(
'
watch count cleanup result: watch count cleanup: 1
'
)
...
...
@@ -55,14 +43,9 @@ describe('watchEffect', () => {
await
incrementBtn
.
tap
()
expect
(
await
count
.
text
()).
toBe
(
'
count: 2
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): count: 1
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): count: 1
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 3
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 3
'
)
expect
(
await
watchCountCleanupRes
.
text
()).
toBe
(
'
watch count cleanup result: watch count cleanup: 2
'
)
...
...
@@ -76,14 +59,9 @@ describe('watchEffect', () => {
await
incrementBtn
.
tap
()
expect
(
await
count
.
text
()).
toBe
(
'
count: 3
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): count: 1
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, count ref text (flush sync): count: 1
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 3
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 3
'
)
expect
(
await
watchCountCleanupRes
.
text
()).
toBe
(
'
watch count cleanup result: watch count cleanup: 2
'
)
...
...
@@ -110,23 +88,13 @@ describe('watchEffect', () => {
'
watch obj result: obj: {"num":0,"str":"num: 0","bool":false,"arr":[0]}
'
)
}
const
watchObjStrRes
=
await
page
.
$
(
'
#watch-obj-str-res
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 0, obj.str ref text (flush pre): obj.str: num: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 0, obj.str ref text (flush pre): null
'
)
}
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 0, obj.str ref text (flush pre): obj.str: num: 0
'
)
const
watchObjBoolRes
=
await
page
.
$
(
'
#watch-obj-bool-res
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: false, obj.bool ref text (flush post): obj.bool: false
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: false, obj.bool ref text (flush post): null
'
)
}
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: false, obj.bool ref text (flush post): obj.bool: false
'
)
const
watchObjArrRes
=
await
page
.
$
(
'
#watch-obj-arr-res
'
)
expect
(
await
watchObjArrRes
.
text
()).
toBe
(
'
watch obj.arr result: arr: [0]
'
)
...
...
@@ -146,22 +114,11 @@ describe('watchEffect', () => {
expect
(
await
watchObjRes
.
text
()).
toBe
(
'
watch obj result: obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 1, obj.str ref text (flush pre): obj.str: num: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 1, obj.str ref text (flush pre): null
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, obj.bool ref text (flush post): obj.bool: true
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, obj.bool ref text (flush post): null
'
)
}
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, obj.bool ref text (flush post): obj.bool: true
'
)
expect
(
await
watchObjArrRes
.
text
()).
toBe
(
'
watch obj.arr result: arr: [0,1]
'
)
const
watchCountAndObjNumRes
=
await
page
.
$
(
'
#watch-count-obj-num-res
'
)
...
...
pages/composition-api/reactivity/watch-effect/watch-effect.uvue
浏览文件 @
8d968cfd
...
...
@@ -43,7 +43,7 @@
arr : number[]
}
const countRef = ref<UniElement | null>(null)
const countRef = ref<Uni
Text
Element | null>(null)
const count = ref(0)
const watchCountRes = ref('')
const watchCountCleanupRes = ref('')
...
...
@@ -52,7 +52,12 @@
const stopWatchCount = watchEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) {
watchCountRes.value = `count: ${count.value}, count ref text (flush sync): ${countRef.value!.getAttribute('value')}`
// #ifdef APP
watchCountRes.value = `count: ${count.value}, count ref text (flush sync): ${countRef.value!.value}`
// #endif
// #ifdef WEB
watchCountRes.value = `count: ${count.value}, count ref text (flush sync): ${(countRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchCountRes.value = `count: ${count.value}, count ref text (flush sync): `
}
...
...
@@ -95,11 +100,16 @@
watchObjRes.value = `obj: ${JSON.stringify(obj)}`
})
const objStrRef = ref<UniElement | null>(null)
const objStrRef = ref<Uni
Text
Element | null>(null)
const watchObjStrRes = ref('')
watchEffect(() => {
if (objStrRef.value !== null) {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text (flush pre): ${objStrRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text (flush pre): ${objStrRef.value!.value}`
// #endif
// #ifdef WEB
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text (flush pre): ${(objStrRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text (flush pre): `
}
...
...
@@ -108,11 +118,16 @@
flush: 'pre',
})
const objBoolRef = ref<UniElement | null>(null)
const objBoolRef = ref<Uni
Text
Element | null>(null)
const watchObjBoolRes = ref('')
watchEffect(() => {
if (objBoolRef.value !== null) {
watchObjBoolRes.value = `bool: ${obj.bool}, obj.bool ref text (flush post): ${objBoolRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjBoolRes.value = `bool: ${obj.bool}, obj.bool ref text (flush post): ${objBoolRef.value!.value}`
// #endif
// #ifdef WEB
watchObjBoolRes.value = `bool: ${obj.bool}, obj.bool ref text (flush post): ${(objBoolRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchObjBoolRes.value = `bool: ${obj.bool}, obj.bool ref text (flush post): `
}
...
...
pages/composition-api/reactivity/watch-post-effect/watch-post-effect.uvue
浏览文件 @
8d968cfd
...
...
@@ -42,7 +42,7 @@
arr : number[]
}
const countRef = ref<UniElement | null>(null)
const countRef = ref<Uni
Text
Element | null>(null)
const count = ref<number>(0)
const watchCountRes = ref('')
const watchCountCleanupRes = ref('')
...
...
@@ -51,7 +51,12 @@
const stopWatchCount = watchPostEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) {
watchCountRes.value = `count: ${count.value}, count ref text: ${countRef.value!.getAttribute('value')}`
// #ifdef APP
watchCountRes.value = `count: ${count.value}, count ref text: ${countRef.value!.value}`
// #endif
// #ifdef WEB
watchCountRes.value = `count: ${count.value}, count ref text: ${(countRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchCountRes.value = `count: ${count.value}, count ref text: `
}
...
...
@@ -94,11 +99,16 @@
watchObjRes.value = `obj: ${JSON.stringify(obj)}`
})
const objStrRef = ref<UniElement | null>(null)
const objStrRef = ref<Uni
Text
Element | null>(null)
const watchObjStrRes = ref('')
watchPostEffect(() => {
if (objStrRef.value !== null) {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${objStrRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${objStrRef.value!.value}`
// #endif
// #ifdef WEB
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${(objStrRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: `
}
...
...
pages/composition-api/reactivity/watch-sync-effect/watch-sync-effect.uvue
浏览文件 @
8d968cfd
...
...
@@ -42,7 +42,7 @@
arr : number[]
}
const countRef = ref<UniElement | null>(null)
const countRef = ref<Uni
Text
Element | null>(null)
const count = ref(0)
const watchCountRes = ref('')
const watchCountCleanupRes = ref('')
...
...
@@ -51,12 +51,17 @@
const stopWatchCount = watchSyncEffect((onCleanup : OnCleanup) => {
if (countRef.value !== null) {
watchCountRes.value = `count: ${count.value}, count ref text: ${countRef.value!.getAttribute('value')}`
// #ifdef APP
watchCountRes.value = `count: ${count.value}, count ref text: ${countRef.value!.value}`
// #endif
// #ifdef WEB
watchCountRes.value = `count: ${count.value}, count ref text: ${countRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
} else {
watchCountRes.value = `count: ${
count.value
}, count ref text: `
watchCountRes.value = `count: ${
count.value
}, count ref text: `
}
const cancel = () => {
watchCountCleanupRes.value = `watch count cleanup: ${
count.value
}`
watchCountCleanupRes.value = `watch count cleanup: ${
count.value
}`
}
onCleanup(cancel)
},
...
...
@@ -91,32 +96,37 @@
const watchObjRes = ref('')
watchSyncEffect(() => {
watchObjRes.value = `obj: ${
JSON.stringify(obj)
}`
watchObjRes.value = `obj: ${
JSON.stringify(obj)
}`
})
const objStrRef = ref<UniElement | null>(null)
const objStrRef = ref<Uni
Text
Element | null>(null)
const watchObjStrRes = ref('')
watchSyncEffect(() => {
if (objStrRef.value !== null) {
watchObjStrRes.value = `str: ${obj.str}, obj.str ref text: ${objStrRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjStrRes.value = `str: ${ obj.str }, obj.str ref text: ${ objStrRef.value!.value }`
// #endif
// #ifdef WEB
watchObjStrRes.value = `str: ${ obj.str }, obj.str ref text: ${ (objStrRef.value!.childNodes[0] as HTMLElement).innerText }`
// #endif
} else {
watchObjStrRes.value = `str: ${
obj.str
}, obj.str ref text: `
watchObjStrRes.value = `str: ${
obj.str
}, obj.str ref text: `
}
})
const watchObjArrRes = ref('')
watchSyncEffect(() => {
watchObjArrRes.value = `arr: ${
JSON.stringify(obj.arr)
}`
watchObjArrRes.value = `arr: ${
JSON.stringify(obj.arr)
}`
})
const watchCountAndObjNumRes = ref('')
watchSyncEffect(() => {
watchCountAndObjNumRes.value = `count: ${
count.value}, obj.num: ${obj.num
}`
watchCountAndObjNumRes.value = `count: ${
count.value }, obj.num: ${ obj.num
}`
})
const updateObj = () => {
obj.num++
obj.str = `num: ${
obj.num
}`
obj.str = `num: ${
obj.num
}`
obj.bool = !obj.bool
obj.arr.push(obj.num)
}
...
...
pages/composition-api/reactivity/watch/watch.test.js
浏览文件 @
8d968cfd
...
...
@@ -33,14 +33,8 @@ describe('watch', () => {
expect
(
await
count
.
text
()).
toBe
(
'
count: 1
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, prevCount: 0, count ref text (flush sync): count: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, prevCount: 0, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 1, prevCount: 0, count ref text (flush sync): count: 0
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 2
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 1
'
)
...
...
@@ -52,15 +46,8 @@ describe('watch', () => {
await
incrementBtn
.
tap
()
expect
(
await
count
.
text
()).
toBe
(
'
count: 2
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): count: 1
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): count: 1
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 2
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 2
'
)
expect
(
await
watchCountCleanupRes
.
text
()).
toBe
(
'
watch count cleanup result: watch count cleanup: 1
'
)
...
...
@@ -77,14 +64,8 @@ describe('watch', () => {
await
incrementBtn
.
tap
()
expect
(
await
count
.
text
()).
toBe
(
'
count: 3
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): count: 1
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): null
'
)
}
expect
(
await
watchCountRes
.
text
()).
toBe
(
'
watch count result: count: 2, prevCount: 1, count ref text (flush sync): count: 1
'
)
expect
(
await
watchCountTrackNum
.
text
()).
toBe
(
'
watch count track number: 2
'
)
expect
(
await
watchCountTriggerNum
.
text
()).
toBe
(
'
watch count trigger number: 2
'
)
expect
(
await
watchCountCleanupRes
.
text
()).
toBe
(
'
watch count cleanup result: watch count cleanup: 2
'
)
...
...
@@ -138,24 +119,12 @@ describe('watch', () => {
'
watch obj result: obj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}, prevObj: {"num":1,"str":"num: 1","bool":true,"arr":[0,1]}
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 1, prevStr: num: 0, obj.str ref text (flush pre): obj.str: num: 0
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 1, prevStr: num: 0, obj.str ref text (flush pre): null
'
)
}
expect
(
await
watchObjStrRes
.
text
()).
toBe
(
'
watch obj.str result: str: num: 1, prevStr: num: 0, obj.str ref text (flush pre): obj.str: num: 0
'
)
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, prevBool: false, obj.bool ref text (flush post): obj.bool: true
'
)
}
if
(
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
web
'
))
{
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, prevBool: false, obj.bool ref text (flush post): null
'
)
}
expect
(
await
watchObjBoolRes
.
text
()).
toBe
(
'
watch obj.bool result: bool: true, prevBool: false, obj.bool ref text (flush post): obj.bool: true
'
)
expect
(
await
watchObjArrRes
.
text
()).
toBe
(
'
watch obj.arr result: arr: [0,1], prevArr: [0,1]
'
)
const
watchCountAndObjNumRes
=
await
page
.
$
(
'
#watch-count-obj-num-res
'
)
...
...
pages/composition-api/reactivity/watch/watch.uvue
浏览文件 @
8d968cfd
...
...
@@ -43,7 +43,7 @@
arr : number[]
}
const countRef = ref<UniElement | null>(null)
const countRef = ref<Uni
Text
Element | null>(null)
const count = ref(0)
const watchCountRes = ref('')
const watchCountCleanupRes = ref('')
...
...
@@ -51,7 +51,12 @@
const watchCountTriggerNum = ref(0)
const stopWatchCount = watch(count, (count : number, prevCount : number, onCleanup : OnCleanup) => {
watchCountRes.value = `count: ${count}, prevCount: ${prevCount}, count ref text (flush sync): ${countRef.value!.getAttribute('value')}`
// #ifdef APP
watchCountRes.value = `count: ${count}, prevCount: ${prevCount}, count ref text (flush sync): ${countRef.value!.value}`
// #endif
// #ifdef WEB
watchCountRes.value = `count: ${count}, prevCount: ${prevCount}, count ref text (flush sync): ${(countRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
const cancel = () => {
watchCountCleanupRes.value = `watch count cleanup: ${count}`
}
...
...
@@ -88,23 +93,33 @@
// immediate: true 第一次触发, 旧值应该是 undefined, 现在 app 是初始值
const watchObjRes = ref('')
watch(obj, (obj : Obj, prevObj?: Obj) => {
watch(obj, (obj : Obj, prevObj
?: Obj) => {
watchObjRes.value = `obj: ${JSON.stringify(obj)}, prevObj: ${JSON.stringify(prevObj)}`
}, { immediate: true })
const objStrRef = ref<UniElement | null>(null)
const objStrRef = ref<Uni
Text
Element | null>(null)
const watchObjStrRes = ref('')
watch(() : string => obj.str, (str : string, prevStr : string) => {
watchObjStrRes.value = `str: ${str}, prevStr: ${prevStr}, obj.str ref text (flush pre): ${objStrRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjStrRes.value = `str: ${str}, prevStr: ${prevStr}, obj.str ref text (flush pre): ${objStrRef.value!.value}`
// #endif
// #ifdef WEB
watchObjStrRes.value = `str: ${str}, prevStr: ${prevStr}, obj.str ref text (flush pre): ${(objStrRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
}, {
// 侦听器在组件渲染之前触发
flush: 'pre',
})
const objBoolRef = ref<UniElement | null>(null)
const objBoolRef = ref<Uni
Text
Element | null>(null)
const watchObjBoolRes = ref('')
watch(() : boolean => obj.bool, (bool : boolean, prevBool : boolean) => {
watchObjBoolRes.value = `bool: ${bool}, prevBool: ${prevBool}, obj.bool ref text (flush post): ${objBoolRef.value!.getAttribute('value')}`
// #ifdef APP
watchObjBoolRes.value = `bool: ${bool}, prevBool: ${prevBool}, obj.bool ref text (flush post): ${objBoolRef.value!.value}`
// #endif
// #ifdef WEB
watchObjBoolRes.value = `bool: ${bool}, prevBool: ${prevBool}, obj.bool ref text (flush post): ${(objBoolRef.value!.childNodes[0] as HTMLElement).innerText}`
// #endif
}, {
// 侦听器延迟到组件渲染之后触发
flush: 'post'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录