index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="custom-tabbar" :class="{ 'safe-area-inset-bottom': true }">
  3. <view
  4. v-for="(item, index) in tabList"
  5. :key="index"
  6. class="tabbar-item"
  7. :class="{ active: currentPath === item.pagePath }"
  8. @click="switchTab(item, index)"
  9. >
  10. <view class="icon-box">
  11. <Icon
  12. v-if="item.iconName"
  13. :name="item.iconName"
  14. :size="48"
  15. :color="currentPath === item.pagePath ? activeColor : inactiveColor"
  16. />
  17. </view>
  18. <text
  19. class="text"
  20. :style="{ color: currentPath === item.pagePath ? activeColor : inactiveColor }"
  21. >{{ item.text }}</text>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup>
  26. import { ref, onMounted, onUnmounted } from 'vue';
  27. import Icon from '@/components/icon/index.vue';
  28. // 固定的导航配置
  29. const tabList = [
  30. {
  31. iconName: 'icon-shouye',
  32. text: '首页',
  33. pagePath: 'pages/index/index'
  34. },
  35. {
  36. iconName: 'icon-zuanshi',
  37. text: '产品',
  38. pagePath: 'pages/product/index'
  39. },
  40. {
  41. iconName: 'icon-zhifeiji',
  42. text: '活动',
  43. pagePath: 'pages/activity/index'
  44. },
  45. {
  46. iconName: 'icon-qizhi',
  47. text: '基(营)地',
  48. pagePath: 'pages/base/index'
  49. },
  50. {
  51. iconName: 'icon-renyuan',
  52. text: '我的',
  53. pagePath: 'pages/my/index'
  54. }
  55. ];
  56. // 颜色配置
  57. const activeColor = '#1d2089';
  58. const inactiveColor = '#999';
  59. // 当前路径
  60. const currentPath = ref('');
  61. // 更新当前路径
  62. const updateCurrentPath = () => {
  63. const pages = getCurrentPages();
  64. const currentPage = pages[pages.length - 1];
  65. currentPath.value = currentPage ? currentPage.route : '';
  66. };
  67. // 切换页面
  68. const switchTab = (item) => {
  69. if (currentPath.value === item.pagePath) return;
  70. uni.switchTab({
  71. url: `/${item.pagePath}`
  72. });
  73. };
  74. // 监听路由变化
  75. uni.$on('onShow', () => {
  76. updateCurrentPath();
  77. });
  78. // 组件挂载时更新路径
  79. onMounted(() => {
  80. updateCurrentPath();
  81. });
  82. // 组件销毁时移除监听
  83. onUnmounted(() => {
  84. uni.$off('onShow');
  85. });
  86. </script>
  87. <style lang="scss" scoped>
  88. .custom-tabbar {
  89. position: fixed;
  90. left: 0;
  91. bottom: 0;
  92. width: 100%;
  93. height: 120rpx;
  94. align-items: center;
  95. background-color: #fff;
  96. display: flex;
  97. border-top: 1rpx solid #dcdcdc;
  98. z-index: 999;
  99. &.safe-area-inset-bottom {
  100. padding-bottom: constant(safe-area-inset-bottom);
  101. padding-bottom: env(safe-area-inset-bottom);
  102. }
  103. .tabbar-item {
  104. flex: 1;
  105. display: flex;
  106. flex-direction: column;
  107. align-items: center;
  108. justify-content: center;
  109. .icon-box {
  110. margin-bottom: 6rpx;
  111. }
  112. .text {
  113. font-size: 24rpx;
  114. line-height: 1;
  115. }
  116. }
  117. }
  118. </style>