爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.2 KiB

2 weeks ago
  1. <template>
  2. <view class="tab-bar">
  3. <view
  4. v-for="(item, index) in tabList"
  5. :key="index"
  6. class="tab-item"
  7. :class="{ active: currentTab === item.pagePath }"
  8. @tap="switchTab(item.pagePath)"
  9. >
  10. <image
  11. :src="currentTab === item.pagePath ? item.selectedIconPath : item.iconPath"
  12. class="tab-icon"
  13. ></image>
  14. <text>{{ item.text }}</text>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'UserTabBar',
  21. data() {
  22. return {
  23. currentTab: '/pages/component/home',
  24. tabList: [
  25. {
  26. pagePath: '/pages/component/home',
  27. text: '首页',
  28. iconPath: '/static/logo.png',
  29. selectedIconPath: '/static/logo.png'
  30. },
  31. {
  32. pagePath: '/pages/component/recycle',
  33. text: '回收',
  34. iconPath: '/static/logo.png',
  35. selectedIconPath: '/static/logo.png'
  36. },
  37. {
  38. pagePath: '/pages/component/my',
  39. text: '我的',
  40. iconPath: '/static/logo.png',
  41. selectedIconPath: '/static/logo.png'
  42. }
  43. ]
  44. }
  45. },
  46. methods: {
  47. switchTab(path) {
  48. if (this.currentTab === path) return
  49. // 立即更新当前选中状态
  50. const oldPath = this.currentTab
  51. this.currentTab = path
  52. // 进行路由跳转
  53. uni.switchTab({
  54. url: path,
  55. success: () => {
  56. // 跳转成功,状态已经更新
  57. console.log('切换成功')
  58. },
  59. fail: () => {
  60. // 如果switchTab失败,尝试navigateTo
  61. uni.navigateTo({
  62. url: path,
  63. success: () => {
  64. console.log('导航成功')
  65. },
  66. fail: () => {
  67. // 如果都失败了,恢复原来的状态
  68. this.currentTab = oldPath
  69. console.log('切换失败')
  70. }
  71. })
  72. }
  73. })
  74. },
  75. updateCurrentTab() {
  76. const pages = getCurrentPages()
  77. if (pages.length > 0) {
  78. const currentPage = pages[pages.length - 1]
  79. const newPath = '/' + currentPage.route
  80. // 只在路径真正改变时才更新
  81. if (this.currentTab !== newPath) {
  82. this.currentTab = newPath
  83. }
  84. }
  85. }
  86. },
  87. created() {
  88. this.updateCurrentTab()
  89. },
  90. onShow() {
  91. this.updateCurrentTab()
  92. },
  93. onLoad() {
  94. // 页面加载时更新当前tab
  95. this.updateCurrentTab()
  96. }
  97. }
  98. </script>
  99. <style lang="scss" scoped>
  100. .tab-bar {
  101. position: fixed;
  102. bottom: 0;
  103. left: 0;
  104. right: 0;
  105. height: 100rpx;
  106. background: #fff;
  107. display: flex;
  108. padding-bottom: env(safe-area-inset-bottom);
  109. border-top: 1px solid #f5f5f5;
  110. z-index: 1000;
  111. .tab-item {
  112. flex: 1;
  113. display: flex;
  114. flex-direction: column;
  115. align-items: center;
  116. justify-content: center;
  117. font-size: 24rpx;
  118. color: #999;
  119. gap: 4rpx;
  120. .tab-icon {
  121. width: 48rpx;
  122. height: 48rpx;
  123. }
  124. &.active {
  125. color: #00C853;
  126. }
  127. }
  128. }
  129. </style>