提交 937d899a 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat(composition api): toRef

上级 363e7bb8
const PAGE_PATH = '/pages/composition-api/reactivity/to-ref/to-ref'
describe('toRef', () => {
if (process.env.uniTestPlatformInfo.startsWith('android')) {
let page = null
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor('view')
})
it('basic', async () => {
const count = await page.$('#count')
expect(await count.text()).toBe('count: 0')
const isRefCount = await page.$('#is-ref-count')
expect(await isRefCount.text()).toBe('isRef count: false')
const refCount = await page.$('#ref-count')
expect(await refCount.text()).toBe('ref count: 0')
const isRefRefCount = await page.$('#is-ref-ref-count')
expect(await isRefRefCount.text()).toBe('isRef ref count: true')
const objNum = await page.$('#obj-num')
expect(await objNum.text()).toBe('obj.num: 0')
const toRefObjNum = await page.$('#to-ref-obj-num')
expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 0')
const toRefFnObjNum = await page.$('#to-ref-fn-obj-num')
expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 0')
const incrementBtn = await page.$('#increment-btn')
await incrementBtn.tap()
expect(await objNum.text()).toBe('obj.num: 2')
expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 2')
expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 2')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template><view class="page">toRef</view></template>
\ No newline at end of file
<template>
<view class="page">
<text id="count">count: {{ count }}</text>
<text class="uni-common-mt" id="is-ref-count">isRef count: {{ isRefCount }}</text>
<text class="uni-common-mt" id="ref-count">ref count: {{ refCount }}</text>
<text class="uni-common-mt" id="is-ref-ref-count">isRef ref count: {{ isRefRefCount }}</text>
<text class="uni-common-mt" id="obj-num">obj.num: {{ obj.num }}</text>
<text class="uni-common-mt" id="to-ref-obj-num">toRef(obj, "num"): {{ objNum }}</text>
<text class="uni-common-mt" id="to-ref-fn-obj-num">toRef(() => obj.num): {{ readonlyObjNum }}</text>
<button class="uni-common-mt" id="increment-btn" @click="increment">increment obj.num</button>
</view>
</template>
<script setup>
const count = 0;
const isRefCount = isRef(count);
const refCount = toRef<number>(count);
const isRefRefCount = isRef(refCount);
type Obj = {
num : number
}
const obj = reactive({
num: 0
} as Obj)
const objNum = toRef<number>(obj, 'num')
const readonlyObjNum = toRef<number>(() : number => obj.num)
const increment = () => {
obj.num++;
objNum.value++;
readonlyObjNum.value++;
}
</script>
\ No newline at end of file
......@@ -136,7 +136,7 @@
{
name: 'toRef',
url: 'to-ref',
enable: false,
enable: true,
},
{
name: 'toValue',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册