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

feat(composition api): useSlots

上级 9d6c3874
<template>
title: {{ title }}
title: {{ title }}
<button class="component-lifecycle-btn" @click="updateTitle">updateTitle</button>
</template>
<script lang="ts">
<script>
import { state, setLifeCycleNum } from '../store/index.uts'
export default {
name: 'ComponentLifecycle',
......@@ -53,8 +53,8 @@ export default {
console.log('component for lifecycle test unmounted')
},
methods: {
updateTitle(){
this.title = 'component for lifecycle test updated'
updateTitle(){
this.title = 'component for lifecycle test updated'
}
},
}
......
<template>
<view class="page">
<slot name="header" :num="num"></slot>
<slot :msg="msg"></slot>
<slot name="footer" :arr="arr"></slot>
<text id="check-use-slots-res" class="uni-common-mt">check useSlots result: {{ checkUseSlotsRes }}</text>
<button id="check-use-slots-btn" class="uni-common-mt" @click="checkUseSlots">
check useSlots
</button>
</view>
</template>
<script setup>
const num = ref<number>(0);
const msg = ref('default msg');
const arr = ref<string[]>(['a', 'b', 'c']);
const slots = defineSlots<{
default(props : { msg : string }) : any;
header(props : { num : number }) : any;
footer(props : { arr : string[] }) : any;
}>();
const slotsByUseSlots = useSlots();
const checkUseSlotsRes = ref(false);
const checkUseSlots = () => {
checkUseSlotsRes.value =
slots === slotsByUseSlots &&
slotsByUseSlots['default'] !== null &&
slotsByUseSlots['header'] !== null &&
slotsByUseSlots['footer'] !== null;
};
</script>
\ No newline at end of file
const PAGE_PATH = '/pages/composition-api/basic/use-slots/use-slots'
describe('useSlots', () => {
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 slotHeader = await page.$('#slot-header')
expect(await slotHeader.text()).toBe('header slot num: 0')
const slotContent = await page.$('#slot-default')
expect(await slotContent.text()).toBe('default slot msg: default msg')
const slotFooter = await page.$('#slot-footer')
expect(await slotFooter.text()).toBe('footer slot arr: ["a","b","c"]')
const checkUseSlotsRes = await page.$('#check-use-slots-res')
expect(await checkUseSlotsRes.text()).toBe('check useSlots result: false')
const checkUseSlotsBtn = await page.$('#check-use-slots-btn')
await checkUseSlotsBtn.tap()
expect(await checkUseSlotsRes.text()).toBe('check useSlots result: true')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template>
<view class="page"> useSlots </view>
<Foo>
<template #header="{num}">
<text id="slot-header">header slot num: {{num}}</text>
</template>
<template #default="{msg}">
<text id="slot-default" class="uni-common-mt">default slot msg: {{msg}}</text>
</template>
<template #footer="{arr}">
<text id="slot-footer" class="uni-common-mt">footer slot arr: {{arr}}</text>
</template>
</Foo>
</template>
<script setup></script>
<script setup>
import Foo from './Foo.uvue'
</script>
\ No newline at end of file
......@@ -75,7 +75,7 @@
{
name: 'useSlots',
url: 'use-slots',
enable: false,
enable: true,
},
{
name: 'useAttrs',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册