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

refactor(component instance): 优化示例及测试

上级 85bd1f66
......@@ -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
......@@ -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
......@@ -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 () => {
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册