149 lines
3.5 KiB
Vue
149 lines
3.5 KiB
Vue
<template>
|
|
<uni-tabbar v-if="hasTabBar" v-show="showTabBar">
|
|
<div
|
|
:style="{
|
|
'flex-direction': direction === 'vertical' ? 'column' : 'row',
|
|
backgroundColor: tabBar.backgroundColor,
|
|
}"
|
|
class="uni-tabbar"
|
|
>
|
|
<template v-for="(item, index) in tabBar.list" :key="item.pagePath">
|
|
<div
|
|
v-if="item.visible !== false"
|
|
class="uni-tabbar__item"
|
|
@click="switchTab(item, index)"
|
|
>
|
|
<div class="uni-tabbar__bd">
|
|
<div
|
|
v-if="showIcon && item.iconPath"
|
|
:class="{ 'uni-tabbar__icon__diff': !item.text }"
|
|
class="uni-tabbar__icon"
|
|
>
|
|
<img
|
|
:src="
|
|
getRealPath(
|
|
selectedIndex === index
|
|
? item.selectedIconPath
|
|
: item.iconPath
|
|
)
|
|
"
|
|
/>
|
|
<div
|
|
v-if="item.redDot"
|
|
:class="{ 'uni-tabbar__badge': !!item.badge }"
|
|
class="uni-tabbar__reddot"
|
|
>
|
|
{{ item.badge }}
|
|
</div>
|
|
</div>
|
|
<div
|
|
v-if="item.text"
|
|
:style="{
|
|
color:
|
|
selectedIndex === index ? tabBar.selectedColor : tabBar.color,
|
|
fontSize: showIcon && item.iconPath ? '10px' : '14px',
|
|
}"
|
|
class="uni-tabbar__label"
|
|
>
|
|
{{ item.text }}
|
|
<div
|
|
v-if="item.redDot && (!showIcon || !item.iconPath)"
|
|
:class="{ 'uni-tabbar__badge': !!item.badge }"
|
|
class="uni-tabbar__reddot"
|
|
>
|
|
{{ item.badge }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</uni-tabbar>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref, watch } from 'vue'
|
|
import { useRoute } from 'vue-router'
|
|
import { getRealPath } from '@dcloudio/uni-h5'
|
|
import { useTabBar } from '@dcloudio/uni-h5'
|
|
|
|
defineOptions({
|
|
name: 'CustomTabBar',
|
|
})
|
|
|
|
const props = defineProps({
|
|
selected: {
|
|
type: Number,
|
|
default: 0,
|
|
},
|
|
showIcon: {
|
|
type: Boolean,
|
|
default: true,
|
|
},
|
|
direction: {
|
|
type: String,
|
|
default: 'horizontal',
|
|
},
|
|
})
|
|
|
|
const emit = defineEmits()
|
|
const tabBar = useTabBar()!
|
|
const route = useRoute()
|
|
const hasTabBar = computed(() => tabBar.list && tabBar.list.length)
|
|
const selectedIndex = ref(props.selected)
|
|
const showTabBar = ref(true)
|
|
watch(
|
|
() => props.selected,
|
|
(value) => (selectedIndex.value = value)
|
|
)
|
|
watch(
|
|
() => selectedIndex.value,
|
|
(value) => (tabBar.selectedIndex = value)
|
|
)
|
|
watch(
|
|
() => {
|
|
const meta = route.meta
|
|
return [meta.isTabBar, meta.route]
|
|
},
|
|
([isTabBar, pagePath]) => {
|
|
if (isTabBar) {
|
|
const index = tabBar.list.findIndex((item) => pagePath === item.pagePath)
|
|
if (index > -1) {
|
|
selectedIndex.value = index
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
function switchTab(item: UniApp.TabBarItemOptions, index: number) {
|
|
selectedIndex.value = index
|
|
const detail = {
|
|
index,
|
|
text: item.text,
|
|
pagePath: item.pagePath,
|
|
}
|
|
emit('onTabItemTap', detail)
|
|
}
|
|
|
|
// 保留迁移前公开实例可访问的 props 和 setup 返回成员。
|
|
defineExpose({
|
|
get selected() {
|
|
return props.selected
|
|
},
|
|
get showIcon() {
|
|
return props.showIcon
|
|
},
|
|
get direction() {
|
|
return props.direction
|
|
},
|
|
tabBar,
|
|
getRealPath,
|
|
selectedIndex,
|
|
hasTabBar,
|
|
showTabBar,
|
|
switchTab,
|
|
})
|
|
</script>
|
|
|
|
<style></style>
|