index.vue 3.3 KB

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