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

feat(v-slot): 补充 v-for 动态插槽名示例

上级 9022b1b2
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
<view> <view>
<slot name="header" :msg="msg"></slot> <slot name="header" :msg="msg"></slot>
<slot :num="num"></slot> <slot :num="num"></slot>
<slot name="num1" :num="num"></slot>
<slot name="num2" :num="num"></slot>
<slot name="msgTrue" :msg="msg"></slot>
<slot name="msgFalse" :msg="msg"></slot>
<slot name="footer" :arr="arr"></slot> <slot name="footer" :arr="arr"></slot>
</view> </view>
</template> </template>
...@@ -14,6 +18,10 @@ ...@@ -14,6 +18,10 @@
defineSlots<{ defineSlots<{
header(props : { msg : string }) : any, header(props : { msg : string }) : any,
default(props : { num : number }) : any, default(props : { num : number }) : any,
num1(props : { num : number }) : any,
num2(props : { num : number }) : any,
msgTrue(props : { msg : string }) : any,
msgFalse(props : { msg : string }) : any,
footer(props : { arr : string[] }) : any footer(props : { arr : string[] }) : any
}>() }>()
</script> </script>
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
<view> <view>
<slot name="header" :msg="msg"></slot> <slot name="header" :msg="msg"></slot>
<slot :num="num"></slot> <slot :num="num"></slot>
<slot name="num1" :num="num"></slot>
<slot name="num2" :num="num"></slot>
<slot name="msgTrue" :msg="msg"></slot>
<slot name="msgFalse" :msg="msg"></slot>
<slot name="footer" :arr="arr"></slot> <slot name="footer" :arr="arr"></slot>
</view> </view>
</template> </template>
...@@ -11,6 +15,10 @@ ...@@ -11,6 +15,10 @@
slots: Object as SlotsType<{ slots: Object as SlotsType<{
header: { msg: string } header: { msg: string }
default: { num: number } default: { num: number }
num1: { num: number }
num2: { num: number }
msgTrue: { msg: string }
msgFalse: { msg: string }
footer: { arr: string[] } footer: { arr: string[] }
}>, }>,
data(){ data(){
......
<template> <template>
<view class="page"> <view class="page">
<Foo> <Foo>
<template #header="{ msg }"> <template #header="{ msg }">
<view class="mb-10 flex justify-between flex-row"> <view class="mb-10 flex justify-between flex-row">
<text>header slot msg:</text> <text>header slot msg:</text>
<text id="slot-header">{{ msg }}</text> <text id="slot-header">{{ msg }}</text>
</view> </view>
</template> </template>
<template #default="{ num }"> <template #default="{ num }">
<view class="mb-10 flex justify-between flex-row"> <view class="mb-10 flex justify-between flex-row">
<text>default slot num:</text> <text>default slot num:</text>
<text id="slot-default">{{ num }}</text> <text id="slot-default">{{ num }}</text>
</view> </view>
</template> </template>
<template #footer="{ arr }"> <template v-for="item in 2" #[`num${item}`]="{ num }">
<view class="mb-10 flex justify-between flex-row"> <view class="mb-10 flex justify-between flex-row">
<text>footer slot arr:</text> <text>num{{ item }} slot:</text>
<text id="slot-footer">{{ JSON.stringify(arr) }}</text> <text :id="`slot-num${item}`">{{ num }}</text>
</view> </view>
</template> </template>
</Foo> <template v-if="msgTrue['isShow']" #[msgTrue['name']]="{ msg }">
</view> <view class="mb-10 flex justify-between flex-row">
</template> <text>{{ msgTrue['name'] }} slot msg:</text>
<text id="slot-msg-true">{{ msg }}</text>
<script setup lang="uts"> </view>
import Foo from './Foo-composition.uvue' </template>
</script> <template v-if="msgFalse['isShow']" #[msgFalse['name']]="{ msg }">
<view class="mb-10 flex justify-between flex-row">
<text>{{ msgFalse['name'] }} slot msg:</text>
<text id="slot-msg-false">{{ msg }}</text>
</view>
</template>
<template #footer="{ arr }">
<view class="mb-10 flex justify-between flex-row">
<text>footer slot arr:</text>
<text id="slot-footer">{{ JSON.stringify(arr) }}</text>
</view>
</template>
</Foo>
</view>
</template>
<script setup lang="uts">
import Foo from './Foo-composition.uvue'
const msgTrue = ref({
isShow: true,
name: 'msgTrue'
})
const msgFalse = ref({
isShow: false,
name: 'msgFalse'
})
</script>
\ No newline at end of file
...@@ -13,6 +13,24 @@ ...@@ -13,6 +13,24 @@
<text id="slot-default">{{ num }}</text> <text id="slot-default">{{ num }}</text>
</view> </view>
</template> </template>
<template v-for="item in 2" #[`num${item}`]="{ num }">
<view class="mb-10 flex justify-between flex-row">
<text>num{{ item }} slot:</text>
<text :id="`slot-num${item}`">{{ num }}</text>
</view>
</template>
<template v-if="msgTrue['isShow']" #[msgTrue['name']]="{ msg }">
<view class="mb-10 flex justify-between flex-row">
<text>{{ msgTrue['name'] }} slot msg:</text>
<text id="slot-msg-true">{{ msg }}</text>
</view>
</template>
<template v-if="msgFalse['isShow']" #[msgFalse['name']]="{ msg }">
<view class="mb-10 flex justify-between flex-row">
<text>{{ msgFalse['name'] }} slot msg:</text>
<text id="slot-msg-false">{{ msg }}</text>
</view>
</template>
<template #footer="{ arr }"> <template #footer="{ arr }">
<view class="mb-10 flex justify-between flex-row"> <view class="mb-10 flex justify-between flex-row">
<text>footer slot arr:</text> <text>footer slot arr:</text>
...@@ -26,6 +44,18 @@ ...@@ -26,6 +44,18 @@
<script lang="uts"> <script lang="uts">
import Foo from './Foo-options.uvue' import Foo from './Foo-options.uvue'
export default { export default {
components: {Foo} components: {Foo},
data(){
return {
msgTrue: {
isShow: true,
name: 'msgTrue'
},
msgFalse: {
isShow: false,
name: 'msgFalse'
}
}
}
} }
</script> </script>
...@@ -11,8 +11,18 @@ describe('v-slot', () => { ...@@ -11,8 +11,18 @@ describe('v-slot', () => {
const slotHeader = await page.$('#slot-header') const slotHeader = await page.$('#slot-header')
expect(await slotHeader.text()).toBe('foo msg') expect(await slotHeader.text()).toBe('foo msg')
const slotContent = await page.$('#slot-default') const slotDefault = await page.$('#slot-default')
expect(await slotContent.text()).toBe('0') expect(await slotDefault.text()).toBe('0')
const slotNum1 = await page.$('#slot-num1')
expect(await slotNum1.text()).toBe('0')
const slotNum2 = await page.$('#slot-num2')
expect(await slotNum2.text()).toBe('0')
const slotMsgTrue = await page.$('#slot-msg-true')
expect(await slotMsgTrue.text()).toBe('foo msg')
const slotMsgFalse = await page.$('#slot-msg-false')
expect(slotMsgFalse).toBe(null)
const slotFooter = await page.$('#slot-footer') const slotFooter = await page.$('#slot-footer')
expect(await slotFooter.text()).toBe('["a","b","c"]') expect(await slotFooter.text()).toBe('["a","b","c"]')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册