Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello-uvue
提交
102617bb
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看板
提交
102617bb
编写于
5月 14, 2024
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(component instance): 优化示例及测试
上级
85bd1f66
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
101 addition
and
33 deletion
+101
-33
pages/component-instance/data/data-composition.uvue
pages/component-instance/data/data-composition.uvue
+41
-13
pages/component-instance/data/data-options.uvue
pages/component-instance/data/data-options.uvue
+47
-20
pages/component-instance/data/data.test.js
pages/component-instance/data/data.test.js
+12
-0
pages/reactivity/core/reactive/reactive.uvue
pages/reactivity/core/reactive/reactive.uvue
+1
-0
未找到文件。
pages/component-instance/data/data-composition.uvue
浏览文件 @
102617bb
...
...
@@ -12,22 +12,50 @@
<text>arr: </text>
<text id="arr">{{ arr.join(',') }}</text>
</view>
<button type="primary" @click="updateData">update data</button>
<view class="flex justify-between flex-row mb-10">
<text>obj.str: </text>
<text id="obj-str">{{ obj.str }}</text>
</view>
<view class="flex justify-between flex-row mb-10">
<text>obj.num: </text>
<text id="obj-num">{{ obj.num }}</text>
</view>
<view class="flex justify-between flex-row mb-10">
<text>obj.arr: </text>
<text id="obj-arr">{{ obj.arr.join(',') }}</text>
</view>
<button @click="updateData">update data</button>
</view>
</template>
<script setup lang="uts">
const str = ref('default str')
const num = ref(0)
const arr = ref([1, 2, 3])
type Obj = {
str : string,
num : number,
arr : number[]
}
const str = ref('default str')
const num = ref(0)
// 可通过泛型指定类型
const arr = ref<number[]>([1, 2, 3])
const obj = ref<Obj>({
str: 'default obj.str',
num: 10,
arr: [4, 5, 6]
})
const updateData = () => {
str.value = 'new str'
num.value = 1
arr.value = [4, 5, 6]
}
const updateData = () => {
str.value = 'new str'
num.value = 1
arr.value = [4, 5, 6]
obj.value.str = 'new obj.str'
obj.value.num = 100
obj.value.arr = [7, 8, 9]
}
defineExpose({
updateData
})
</script>
defineExpose({
updateData
})
</script>
\ No newline at end of file
pages/component-instance/data/data-options.uvue
浏览文件 @
102617bb
...
...
@@ -12,25 +12,52 @@
<text>arr: </text>
<text id="arr">{{ arr.join(',') }}</text>
</view>
<button type="primary" @click="updateData">update data</button>
<view class="flex justify-between flex-row mb-10">
<text>obj.str: </text>
<text id="obj-str">{{ obj.str }}</text>
</view>
<view class="flex justify-between flex-row mb-10">
<text>obj.num: </text>
<text id="obj-num">{{ obj.num }}</text>
</view>
<view class="flex justify-between flex-row mb-10">
<text>obj.arr: </text>
<text id="obj-arr">{{ obj.arr.join(',') }}</text>
</view>
<button @click="updateData">update data</button>
</view>
</template>
</template>
<script lang="uts">
export default {
data() {
return {
str: 'default str',
num: 0,
arr: [1, 2, 3],
}
},
methods: {
updateData() {
this.str = 'new str'
this.num = 1
this.arr = [4, 5, 6]
},
},
}
</script>
type Obj = {
str : string,
num : number,
arr : number[]
}
export default {
data() {
return {
str: 'default str',
num: 0,
arr: [1, 2, 3],
// 特殊类型需要通过 as 指定类型
obj: {
str: 'default obj.str',
num: 10,
arr: [4, 5, 6]
} as Obj
}
},
methods: {
updateData() {
this.str = 'new str'
this.num = 1
this.arr = [4, 5, 6]
this.obj.str = 'new obj.str'
this.obj.num = 100
this.obj.arr = [7, 8, 9]
},
},
}
</script>
\ No newline at end of file
pages/component-instance/data/data.test.js
浏览文件 @
102617bb
...
...
@@ -13,11 +13,23 @@ describe('$data', () => {
const
arr
=
await
page
.
$
(
'
#arr
'
)
expect
(
await
arr
.
text
()).
toBe
(
'
1,2,3
'
)
const
objStr
=
await
page
.
$
(
'
#obj-str
'
)
expect
(
await
objStr
.
text
()).
toBe
(
'
default obj.str
'
)
const
objNum
=
await
page
.
$
(
'
#obj-num
'
)
expect
(
await
objNum
.
text
()).
toBe
(
'
10
'
)
const
objArr
=
await
page
.
$
(
'
#obj-arr
'
)
expect
(
await
objArr
.
text
()).
toBe
(
'
4,5,6
'
)
await
page
.
callMethod
(
'
updateData
'
)
expect
(
await
str
.
text
()).
toBe
(
'
new str
'
)
expect
(
await
num
.
text
()).
toBe
(
'
1
'
)
expect
(
await
arr
.
text
()).
toBe
(
'
4,5,6
'
)
expect
(
await
objStr
.
text
()).
toBe
(
'
new obj.str
'
)
expect
(
await
objNum
.
text
()).
toBe
(
'
100
'
)
expect
(
await
objArr
.
text
()).
toBe
(
'
7,8,9
'
)
}
it
(
'
$data 选项式 API
'
,
async
()
=>
{
...
...
pages/reactivity/core/reactive/reactive.uvue
浏览文件 @
102617bb
...
...
@@ -23,6 +23,7 @@
<script setup lang="uts">
const count = ref(0)
// TODO: 待支持后补充泛型示例
const obj = reactive({
str: 'default str',
num: count,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录