Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello-uvue
提交
1b8d992f
H
hello-uvue
项目概览
DCloud
/
hello-uvue
通知
362
Star
3
Fork
8
代码
文件
提交
分支
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看板
提交
1b8d992f
编写于
9月 13, 2023
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
watch-function: 增加复杂情况
上级
9941fe78
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
132 addition
and
63 deletion
+132
-63
pages/component-instance/watch-function/child.uvue
pages/component-instance/watch-function/child.uvue
+2
-2
pages/component-instance/watch-function/watch-function.test.js
.../component-instance/watch-function/watch-function.test.js
+50
-10
pages/component-instance/watch-function/watch-function.uvue
pages/component-instance/watch-function/watch-function.uvue
+80
-51
未找到文件。
pages/component-instance/watch-function/child.uvue
浏览文件 @
1b8d992f
...
...
@@ -27,7 +27,7 @@
val: "init",
changed: false,
immediateValue: 'value',
immediateChanged: false
,
immediateChanged: false
}
},
mounted() {
...
...
@@ -43,7 +43,7 @@
this.val = 'changed'
},
createImmediateWatch() {
this.$watch('immediate
Watch
Value', () => {
this.$watch('immediateValue', () => {
this.immediateChanged = !this.immediateChanged
}, {
immediate: true
...
...
pages/component-instance/watch-function/watch-function.test.js
浏览文件 @
1b8d992f
...
...
@@ -8,27 +8,67 @@ describe('$watch()', () => {
})
it
(
'
$watch() 生效
'
,
async
()
=>
{
const
initValue
=
(
await
page
.
$
(
'
.init-value
'
)).
text
()
const
value
=
await
(
await
page
.
data
()).
val
const
isChange
=
await
(
await
page
.
data
()).
changed
// 验证立即触发
const
value4_new
=
await
page
.
$
(
'
.value-4-n
'
)
const
value4_old
=
await
page
.
$
(
'
.value-4-o
'
)
expect
(
await
value4_new
.
text
()).
toBe
(
'
6
'
)
expect
(
await
value4_old
.
text
()).
toBe
(
'
6
'
)
expect
(
value
).
not
.
toBe
(
initValue
)
expect
(
isChange
).
toBe
(
true
)
const
btn
=
await
page
.
$
(
'
.btn-click
'
)
const
isChange2
=
await
(
await
page
.
data
()).
immediateChanged
expect
(
isChange2
).
toBe
(
true
)
// 改变 10 次
for
(
let
i
=
1
;
i
<
10
;
i
++
)
{
await
btn
.
tap
()
await
page
.
waitFor
(
50
)
await
clickCount
(
page
,
i
)
}
})
it
(
'
子组件 $watch() 生效
'
,
async
()
=>
{
const
initValue
=
(
await
page
.
$
(
'
.child-init-value
'
)).
text
(
)
const
initValue
=
await
page
.
$
(
'
.child-init-value
'
)
const
comp
=
await
page
.
$
(
'
.watch-child
'
)
const
value
=
await
(
await
comp
.
data
()).
val
const
isChange
=
await
(
await
comp
.
data
()).
changed
expect
(
value
).
not
.
toBe
(
initValue
)
expect
(
value
).
not
.
toBe
(
'
init
'
)
expect
(
isChange
).
toBe
(
true
)
const
isChange2
=
await
(
await
comp
.
data
()).
immediateChanged
expect
(
isChange2
).
toBe
(
true
)
})
})
\ No newline at end of file
})
async
function
clickCount
(
page
,
count
)
{
// 验证数据
const
data
=
await
page
.
data
()
const
data_a
=
data
.
a
const
data_b
=
data
.
b
const
data_d
=
data
.
c
.
d
const
data_e
=
data
.
e
const
data_f
=
data
.
f
expect
(
data_a
).
toBe
(
1
+
count
)
expect
(
data_b
).
toBe
(
2
+
count
)
expect
(
data_d
).
toBe
(
4
+
count
)
expect
(
data_e
).
toBe
(
5
+
count
)
expect
(
data_f
).
toBe
(
6
+
count
)
// 验证界面
const
value1_new
=
await
page
.
$
(
'
.value-1-n
'
)
const
value1_old
=
await
page
.
$
(
'
.value-1-o
'
)
const
value2_new
=
await
page
.
$
(
'
.value-2-n
'
)
const
value2_old
=
await
page
.
$
(
'
.value-2-o
'
)
const
value3_new
=
await
page
.
$
(
'
.value-3-n
'
)
const
value3_old
=
await
page
.
$
(
'
.value-3-o
'
)
const
value4_new
=
await
page
.
$
(
'
.value-4-n
'
)
const
value4_old
=
await
page
.
$
(
'
.value-4-o
'
)
const
new_count
=
count
const
old_count
=
count
-
1
expect
(
await
value1_new
.
text
()).
toBe
(
1
+
new_count
+
''
)
expect
(
await
value1_old
.
text
()).
toBe
(
1
+
old_count
+
''
)
expect
(
await
value2_new
.
text
()).
toBe
(
4
+
new_count
+
''
)
expect
(
await
value2_old
.
text
()).
toBe
(
4
+
old_count
+
''
)
expect
(
await
value3_new
.
text
()).
toBe
(
1
+
new_count
+
2
+
new_count
+
''
)
expect
(
await
value3_old
.
text
()).
toBe
(
1
+
old_count
+
2
+
old_count
+
''
)
expect
(
await
value4_new
.
text
()).
toBe
(
6
+
new_count
+
''
)
expect
(
await
value4_old
.
text
()).
toBe
(
6
+
old_count
+
''
)
}
\ No newline at end of file
pages/component-instance/watch-function/watch-function.uvue
浏览文件 @
1b8d992f
<template>
<view class="page">
<view class="row">
<text>初始值</text>
<text class="child-init-value">init</text>
</view>
<view class="row">
<text>val</text>
<text>{{ val }}</text>
</view>
<view class="row">
<text>值是否变化</text>
<text>{{ changed }}</text>
</view>
<view class="row">
<text>immediate=true 值应为 true</text>
<text>{{ immediateChanged }}</text>
<button class="btn-click" type="default" @click="click">Click</button>
<view>
<view class="value-item">
<text class="value-item-k">a</text>
<text class="value-item-v value-1-n">{{v1New}}</text>
<text class="value-item-v value-1-o">{{v1Old}}</text>
</view>
<view class="value-item">
<text class="value-item-k">c.d</text>
<text class="value-item-v value-2-n">{{v2New}}</text>
<text class="value-item-v value-2-o">{{v2Old}}</text>
</view>
<view class="value-item">
<text class="value-item-k">a+b</text>
<text class="value-item-v value-3-n">{{v3New}}</text>
<text class="value-item-v value-3-o">{{v3Old}}</text>
</view>
<view class="value-item">
<text class="value-item-k">f</text>
<text class="value-item-v value-4-n">{{v4New}}</text>
<text class="value-item-v value-4-o">{{v4Old}}</text>
</view>
</view>
<watch-child class="watch-child"></watch-child>
</view>
...
...
@@ -22,58 +29,80 @@
<script>
import child from './child.uvue'
type C = {
d : number
}
export default {
components: {
watchChild: child
},
data() {
return {
val: "init",
changed: false,
immediateValue: 'value',
immediateChanged: false,
// a: 'a',
// b: 'b',
// abChanged: false
a: 1,
b: 2,
c: {
d: 4
} as C,
e: 5,
f: 6,
v1New: 0,
v1Old: 0,
v2New: 0,
v2Old: 0,
v3New: 0,
v3Old: 0,
v4New: 0,
v4Old: 0,
}
},
mounted() {
this.createWatch()
this.createImmediateWatch()
// this.createGetterWatch()
this.$watch('a', (newVal, oldVal) => {
this.v1New = newVal
this.v1Old = oldVal
})
this.$watch('c.d', (newVal : number, oldVal : number) => {
this.v2New = newVal
this.v2Old = oldVal
})
this.$watch(() : number => this.a + this.b, (newVal : number, oldVal : number) => {
this.v3New = newVal
this.v3Old = oldVal
})
this.$watch('f', (newVal, oldVal) => {
this.v4New = newVal
this.v4Old = oldVal
}, { immediate: true })
},
methods: {
createWatch() {
this.$watch('val', () => {
this.changed = !this.changed
})
this.val = 'changed'
},
createImmediateWatch() {
this.$watch('immediateWatchValue', () => {
this.immediateChanged = !this.immediateChanged
}, {
immediate: true
})
},
// createGetterWatch () {
// this.$watch(() => this.a + this.b, () => {
// this.abChanged = !this.abChanged
// })
//
// this.a = 'changed'
// }
click() {
this.a++
this.b++
this.c.d++
this.e++
this.f++
}
}
}
</script>
<style>
.row {
display: flex;
.value {
flex: 1;
flex-direction: column;
}
.value-item {
flex-direction: row;
justify-content: space-between;
margin-bottom: 10px;
}
.value-item-k {
width: 50px;
line-height: 2;
}
.value-item-v {
font-weight: bold;
line-height: 2;
margin-left: 10px;
}
</style>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录