index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!-- custom-tab-bar/index.vue -->
  2. <template>
  3. <view class="custom-tab-bar" v-if="show">
  4. <view class="tab-bar-item" v-for="(item, index) in list" :key="index" @click="switchTab(item, index)">
  5. <text class="tab-text" :class="{ active: selectedIndex == index }">
  6. {{ item.text }}
  7. </text>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. props: {
  14. show: {
  15. type: Boolean,
  16. default: true
  17. },
  18. currentIndex: {
  19. type: Number,
  20. default: 0
  21. }
  22. },
  23. data() {
  24. return {
  25. selectedIndex: 0, // 改个名字避免混淆
  26. list: [{
  27. pagePath: '/pages/chanpinXuanze/index',
  28. text: "版本0",
  29. iconPath: 'static/images/tabbar/unselect/xuanke-sj.png',
  30. selectedIconPath: 'static/images/tabbar/select/xuanke-sj.png'
  31. },
  32. {
  33. pagePath: '/pages/chanpinneirong/index',
  34. text: "学习1",
  35. iconPath: 'static/images/tabbar/unselect/xuexi-sj.png',
  36. selectedIconPath: 'static/images/tabbar/select/xuexi-sj.png'
  37. },
  38. {
  39. pagePath: '/pages/game/index',
  40. text: "游戏2",
  41. iconPath: 'static/images/tabbar/unselect/faxian-sj.png',
  42. selectedIconPath: 'static/images/tabbar/select/faxian-sj.png'
  43. },
  44. {
  45. pagePath: '/pages/chanpinMy/my',
  46. text: "我的3",
  47. iconPath: 'static/images/tabbar/unselect/wode-sj.png',
  48. selectedIconPath: 'static/images/tabbar/select/wode-sj.png'
  49. }
  50. ]
  51. };
  52. },
  53. watch: {
  54. currentIndex: {
  55. handler(newVal) {
  56. console.log('接收到父组件传递的currentIndex:', newVal);
  57. this.selectedIndex = newVal;
  58. },
  59. immediate: true
  60. }
  61. },
  62. methods: {
  63. switchTab(item, index) {
  64. console.log('点击Tab,index:', index, '当前选中的selectedIndex:', this.selectedIndex);
  65. // 如果点击的是当前已选中的Tab,不执行跳转
  66. if (this.selectedIndex === index) {
  67. console.log('点击的是当前已选中的Tab,不跳转');
  68. return;
  69. }
  70. console.log('执行跳转到:', item.pagePath);
  71. // 注意:这里不要更新 selectedIndex,因为跳转后目标页面会传递新的currentIndex
  72. uni.switchTab({
  73. url: item.pagePath
  74. });
  75. }
  76. },
  77. // 可以添加mounted生命周期,打印初始状态
  78. mounted() {
  79. console.log('TabBar组件mounted,初始currentIndex:', this.currentIndex);
  80. }
  81. };
  82. </script>
  83. <style scoped>
  84. /* 样式保持不变 */
  85. .custom-tab-bar {
  86. position: fixed;
  87. bottom: 0;
  88. left: 0;
  89. right: 0;
  90. height: 100rpx;
  91. background: #FFFFFF;
  92. display: flex;
  93. justify-content: space-around;
  94. align-items: center;
  95. border-top: 1rpx solid #F0F0F0;
  96. z-index: 999;
  97. padding-bottom: env(safe-area-inset-bottom);
  98. }
  99. .tab-bar-item {
  100. display: flex;
  101. flex-direction: column;
  102. align-items: center;
  103. justify-content: center;
  104. }
  105. .tab-icon {
  106. width: 44rpx;
  107. height: 44rpx;
  108. margin-bottom: 6rpx;
  109. }
  110. .tab-text {
  111. font-size: 20rpx;
  112. color: #7A7E83;
  113. }
  114. .tab-text.active {
  115. color: #007AFF;
  116. }
  117. </style>