barNavigasiHandap.vue 3.9 KB

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