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

feat(composition api): defineExpose

上级 525cad9a
<script setup>
const str = 'foo str'
const num = ref(0)
const increment = () => {
num.value++
}
defineExpose({
str,
num,
increment
})
</script>
const PAGE_PATH = '/pages/composition-api/basic/define-expose/define-expose'
describe('defineExpose', () => {
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 fooStr = await page.$('#foo-str')
expect(await fooStr.text()).toBe('str from component Foo: foo str')
const fooNum = await page.$('#foo-num')
expect(await fooNum.text()).toBe('num from component Foo: 0')
const incrementBtn = await page.$('#increment-btn')
await incrementBtn.tap()
expect(await fooNum.text()).toBe('num from component Foo: 1')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template> <template>
<view class="page"> defineExpose </view> <view class="page">
<define-expose-foo ref='fooRef' />
<text id="foo-str" class="uni-common-mt">str from component Foo: {{fooStr}}</text>
<text id="foo-num" class="uni-common-mt">num from component Foo: {{fooNum}}</text>
<button id="increment-btn" class="uni-common-mt" @click="increment">trigger Foo increment</button>
</view>
</template> </template>
<script setup></script> <script setup>
const fooRef = ref<DefineExposeFooComponentPublicInstance | null>(null)
const fooStr = ref('')
const fooNum = ref<number>(0)
onMounted(() => {
fooStr.value = fooRef.value!.str
fooNum.value = fooRef.value!.num
})
const increment = () => {
fooRef.value!.increment()
fooNum.value = fooRef.value!.num
}
</script>
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
{ {
name: 'defineExpose', name: 'defineExpose',
url: 'define-expose', url: 'define-expose',
enable: false, enable: true,
}, },
{ {
name: 'defineOptions', name: 'defineOptions',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册