具体参考 动态插槽名问题讨论和 HACK 方案
效果
注意 微信小程序 动态插槽不支持默认内容
子组件 创建插槽
关键处代码
<template v-for="(item, index) in tabItem">
<view class="flex-grow h-0 overflow-auto" v-if="computedValue === item.name" :key="index">
<!-- #ifdef MP -->
<!-- <slot name="{{item.name}}"> </slot> -->
<slot name="tab:{{index}}"> </slot>
<!-- #endif -->
<!-- #ifdef H5 || APP-PLUS -->
<!-- <slot :name="item.name"> -->
<slot :name="`tab:${index}`">
<view class="w-full h-80 flex-center">
<text class="text-c_gray-2">暂无数据{{ item.name }}</text>
</view>
</slot>
<!-- #endif -->
</view>
</template>
完整代码 (样式缺失 使用的是tailwindcss)
<template>
<view class="w-full h-full flex flex-col">
<view class="w-full bg_gradient_blue_4 flex flex-row items-center justify-start" style="height: 83rpx">
<view
v-for="(item, index) in tabItem"
:key="index"
style="height: 83rpx"
class="flex-center flex-row rounded-t-20"
:style="computedWidthStyle"
:class="computedValue === item.name ? 'bg_withe_blue text-c_blue-2' : 'text-c_blue-3'"
@click="changeTab(item)"
>
<text>{{ item.label }}</text>
</view>
</view>
<template v-for="(item, index) in tabItem">
<view class="flex-grow h-0 overflow-auto" v-if="computedValue === item.name" :key="index">
<!-- #ifdef MP -->
<!-- <slot name="{{item.name}}"> </slot> -->
<slot name="tab:{{index}}"> </slot>
<!-- #endif -->
<!-- #ifdef H5 || APP-PLUS -->
<!-- <slot :name="item.name"> -->
<slot :name="`tab:${index}`">
<view class="w-full h-80 flex-center">
<text class="text-c_gray-2">暂无数据{{ item.name }}</text>
</view>
</slot>
<!-- #endif -->
</view>
</template>
</view>
</template>
<script>
export default {
props: {
value: {
default: ''
},
tabItem: {
type: Array,
default: () => [
]
}
},
data: () => ({}),
computed: {
computedValue: {
get() {
return this.value
},
set(value) {
this.$emit('input', value)
}
},
computedWidthStyle() {
let length = this.tabItem.length
if (length <= 0) {
return ''
}
let w = ''
w = Math.floor(100 / length).toFixed(5)
return `width:${w}%;`
}
},
methods: {
changeTab(item) {
this.$emit('input', item.name)
}
},
watch: {},
mounted() {},
beforeUpdate() {},
updated() {},
activated() {},
deactivated() {},
beforeDestroy() {}
}
</script>
<style></style>
父组件 使用插槽
使用关键代码
<!-- #ifdef MP -->
<template v-for="(item, index) in tabItem" slot="tab:{{index}}">
<!-- #endif -->
<!-- #ifdef H5 || APP-PLUS -->
<template v-for="(item, index) in tabItem" :slot="`tab:${index}`">
<!-- #endif -->
<!-- 作业中 -->
<view :key="index" v-if="getIndexToName(tabItem,'working',index)"
>1少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所所所所所所所所2+{{ item.name }}</view
>
</template>
完整代码 (样式缺失 使用的是tailwindcss)
<template>
<view class="w-full flex flex-col" :style="getTabStyle">
<wmsTabs class="w-full h-0 flex-grow " :tabItem="tabItem" v-model="tabValue">
<!-- #ifdef MP -->
<template v-for="(item, index) in tabItem" slot="tab:{{index}}">
<!-- #endif -->
<!-- #ifdef H5 || APP-PLUS -->
<template v-for="(item, index) in tabItem" :slot="`tab:${index}`">
<!-- #endif -->
<!-- 作业中 -->
<view :key="index" v-if="getIndexToName(tabItem,'working',index)"
>1少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所所所所所所所所2+{{ item.name }}</view
>
</template>
</wmsTabs>
</view>
</template>
<script>
import wmsTabs from '@/components/base-ui/wms-tabs'
export default {
components: {
wmsTabs
},
props: {
maxHeight: Number | String
},
data: () => ({
tabValue: 'working',
tabItem: [
{
label: '作业中',
name: 'working',
query: {
name: 'tabItemName',
value: 'working'
}
},
{
label: '待拣',
name: 'waiting',
query: {
name: 'tabItemName',
value: 'waiting'
}
},
{
label: '已拣',
name: 'already',
query: {
name: 'tabItemName',
value: 'already'
}
},
{
label: '拣货详情',
name: 'details',
query: {
name: 'tabItemName',
value: 'details'
}
}
]
}),
computed: {
getTabStyle() {
if (this.maxHeight && this.maxHeight >= 0) {
return `height:${this.maxHeight}px;`
}
}
},
methods: {
getIndexToName(list,name,currentIndex){
let index_ = '';
for (let index = 0; index < list.length; index++) {
const item = list[index];
if(item.name===name){
index_ = index;
break;
}
}
return index_ === currentIndex;
}
},
watch: {},
mounted() {},
beforeUpdate() {},
updated() {},
activated() {},
deactivated() {},
beforeDestroy() {}
}
</script>
<style></style>
|