123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <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>
- import { computed, ref, watch } from 'vue'
- import { useRoute } from 'vue-router'
- import { getRealPath } from '@dcloudio/uni-h5'
- import { useTabBar } from '@dcloudio/uni-h5'
- export default {
- name: 'CustomTabBar',
- props: {
- selected: {
- type: Number,
- default: 0
- },
- showIcon: {
- type: Boolean,
- default: true
- },
- direction: {
- type: String,
- default: 'horizontal'
- }
- },
- setup(props, { emit }) {
- const tabBar = useTabBar()
- const route = useRoute()
- const hasTabBar = computed(() => tabBar.list && tabBar.list.length)
- const selectedIndex = ref(props.selected)
- 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, index) {
- selectedIndex.value = index
- const detail = {
- index,
- text: item.text,
- pagePath: item.pagePath,
- }
- emit('onTabItemTap', detail)
- }
- return {
- tabBar,
- getRealPath,
- selectedIndex,
- hasTabBar,
- showTabBar: true,
- switchTab,
- }
- }
- }
- </script>
- <style>
- </style>
|