barNavigasiHandap.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <!-- 底部导航栏 -->
  3. <view class="bottom-tabbar">
  4. <view class="tabbar-container">
  5. <view
  6. v-for="(item,index) in list" :key="index"
  7. class="tab-item"
  8. :class="{ 'active':currentTab === item.pathText }"
  9. @click="switchTab(item.pathText)"
  10. >
  11. <image
  12. class="tab-icon"
  13. :src="item.pathText === currentTab ?item.oneIcon : item.twoIcon"
  14. mode="aspectFit"
  15. ></image>
  16. <text class="tab-text" :class="{ 'active-text': currentTab === item.pathText }">{{item.name}}</text>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'BarNavigasiHandap',
  24. data() {
  25. return {
  26. currentTab: 'index' ,// 当前选中的tab
  27. list:[
  28. {
  29. name:'首页',
  30. pathText:'index',
  31. oneIcon:'/static/images/tabbar/home_.png',
  32. twoIcon:'/static/images/tabbar/home.png',
  33. },
  34. {
  35. name:'我的',
  36. pathText:'mine',
  37. oneIcon:'/static/images/tabbar/mine_.png',
  38. twoIcon:'/static/images/tabbar/mine.png',
  39. }
  40. ]
  41. }
  42. },
  43. mounted() {
  44. // 组件挂载时设置当前tab
  45. this.setCurrentTab()
  46. // 监听页面显示事件
  47. uni.$on('pageShow', () => {
  48. this.setCurrentTab()
  49. })
  50. },
  51. beforeDestroy() {
  52. // 组件销毁前移除监听
  53. uni.$off('pageShow')
  54. },
  55. methods: {
  56. // 设置当前tab
  57. setCurrentTab() {
  58. try {
  59. const pages = getCurrentPages()
  60. if (pages && pages.length > 0) {
  61. const currentPage = pages[pages.length - 1]
  62. const route = currentPage.route || ''
  63. console.log('当前路由:', route) // 调试用
  64. // 更精确的路由匹配
  65. if (route === 'pages/indexOrder/index') {
  66. this.currentTab = 'index'
  67. } else if (route === 'pages/mine/index') {
  68. this.currentTab = 'mine'
  69. } else if (route.includes('mine')) {
  70. // 如果路由包含 mine,也设置为 mine(处理子页面)
  71. this.currentTab = 'mine'
  72. } else if (route.includes('index') && !route.includes('mine')) {
  73. // 如果路由包含 index 但不包含 mine,设置为 index
  74. this.currentTab = 'index'
  75. }
  76. }
  77. } catch (e) {
  78. console.error('获取当前页面失败:', e)
  79. }
  80. },
  81. // 切换tab
  82. switchTab(tab) {
  83. // 如果点击的是当前tab,不执行操作
  84. if (this.currentTab === tab) {
  85. return
  86. }
  87. // 先更新状态,立即高亮(关键:在跳转前更新状态)
  88. this.currentTab = tab
  89. // 跳转到对应页面(使用 reLaunch 关闭所有页面并跳转)
  90. const urlMap = {
  91. 'index': '/pages/indexOrder/index',
  92. 'mine': '/pages/mine/index'
  93. }
  94. const targetUrl = urlMap[tab]
  95. if (targetUrl) {
  96. uni.reLaunch({
  97. url: targetUrl,
  98. success: () => {
  99. // 跳转成功后再次确认状态
  100. setTimeout(() => {
  101. this.setCurrentTab()
  102. }, 100)
  103. },
  104. fail: (err) => {
  105. console.error('页面跳转失败:', err)
  106. // 跳转失败时恢复状态
  107. this.setCurrentTab()
  108. }
  109. })
  110. }
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .bottom-tabbar {
  117. position: fixed;
  118. bottom: 0;
  119. left: 0;
  120. right: 0;
  121. width: 100%;
  122. background-color: #F5F5F5;
  123. padding: 16rpx 24rpx;
  124. padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  125. z-index: 98;
  126. box-sizing: border-box;
  127. }
  128. .tabbar-container {
  129. display: flex;
  130. justify-content: space-evenly;
  131. align-items: center;
  132. width: 344rpx;
  133. margin: auto;
  134. height: 115rpx;
  135. background: #FFFFFF;
  136. box-shadow: 0rpx 8rpx 38rpx 0rpx rgba(40,86,199,0.2);
  137. border-radius: 57rpx 57rpx 57rpx 57rpx;
  138. }
  139. .tab-item {
  140. font-weight: 500;
  141. font-size: 23rpx;
  142. color: #AAAAAA;
  143. display: flex;
  144. flex-direction: column;
  145. align-items: center;
  146. justify-content: center;
  147. min-height: 100rpx;
  148. }
  149. .tab-item.active {
  150. font-size: 23rpx;
  151. color: #323232;
  152. }
  153. .tab-icon {
  154. width: 49.62rpx;
  155. height: 49.62rpx;
  156. margin-bottom: 6rpx;
  157. }
  158. .tab-text {
  159. font-size: 26rpx;
  160. color: #999999;
  161. line-height: 1;
  162. }
  163. .tab-text.active-text {
  164. color: #323232;
  165. font-weight: 500;
  166. }
  167. </style>