index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <script setup lang="ts">
  2. // 'i-carbon-code',
  3. import { customTabbarList as _tabBarList, customTabbarEnable, nativeTabbarNeedHide, tabbarCacheEnable } from './config'
  4. import { tabbarStore } from './store'
  5. // #ifdef MP-WEIXIN
  6. // 将自定义节点设置成虚拟的(去掉自定义组件包裹层),更加接近Vue组件的表现,能更好的使用flex属性
  7. defineOptions({
  8. virtualHost: true,
  9. })
  10. // #endif
  11. /** tabbarList 里面的 path 从 pages.config.ts 得到 */
  12. const tabbarList = _tabBarList.map(item => ({ ...item, path: `/${item.pagePath}` }))
  13. function handleClick(index: number) {
  14. // 点击原来的不做操作
  15. if (index === tabbarStore.curIdx) {
  16. return
  17. }
  18. const url = tabbarList[index].path
  19. tabbarStore.setCurIdx(index)
  20. if (tabbarCacheEnable) {
  21. uni.switchTab({ url })
  22. }
  23. else {
  24. uni.navigateTo({ url })
  25. }
  26. }
  27. onLoad(() => {
  28. // 解决原生 tabBar 未隐藏导致有2个 tabBar 的问题
  29. nativeTabbarNeedHide
  30. && uni.hideTabBar({
  31. fail(err) {
  32. console.log('hideTabBar fail: ', err)
  33. },
  34. success(res) {
  35. console.log('hideTabBar success: ', res)
  36. },
  37. })
  38. })
  39. const activeColor = '#1890ff'
  40. const inactiveColor = '#666'
  41. function getColorByIndex(index: number) {
  42. return tabbarStore.curIdx === index ? activeColor : inactiveColor
  43. }
  44. function getImageByIndex(index: number, item: { iconActive?: string, icon: string }) {
  45. if (!item.iconActive) {
  46. console.warn('image 模式下,需要配置 iconActive,否则无法切换图片')
  47. return item.icon
  48. }
  49. return tabbarStore.curIdx === index ? item.iconActive : item.icon
  50. }
  51. </script>
  52. <template>
  53. <view v-if="customTabbarEnable" class="h-50px pb-safe">
  54. <view class="border-and-fixed bg-white" @touchmove.stop.prevent>
  55. <view class="h-50px flex items-center">
  56. <view
  57. v-for="(item, index) in tabbarList" :key="index"
  58. class="flex flex-1 flex-col items-center justify-center"
  59. :style="{ color: getColorByIndex(index) }"
  60. @click="handleClick(index)"
  61. >
  62. <template v-if="item.iconType === 'uniUi'">
  63. <uni-icons :type="item.icon" size="20" :color="getColorByIndex(index)" />
  64. </template>
  65. <template v-if="item.iconType === 'uiLib'">
  66. <!-- TODO: 以下内容请根据选择的UI库自行替换 -->
  67. <!-- 如:<wd-icon name="home" /> (https://wot-design-uni.cn/component/icon.html) -->
  68. <!-- 如:<uv-icon name="home" /> (https://www.uvui.cn/components/icon.html) -->
  69. <!-- 如:<sar-icon name="image" /> (https://sard.wzt.zone/sard-uniapp-docs/components/icon)(sar没有home图标^_^) -->
  70. <wd-icon :name="item.icon" size="20" />
  71. </template>
  72. <template v-if="item.iconType === 'unocss' || item.iconType === 'iconfont'">
  73. <view :class="item.icon" class="text-20px" />
  74. </template>
  75. <template v-if="item.iconType === 'image'">
  76. <image :src="getImageByIndex(index, item)" mode="scaleToFill" class="h-20px w-20px" />
  77. </template>
  78. <view class="mt-2px text-12px">
  79. {{ item.text }}
  80. </view>
  81. </view>
  82. </view>
  83. <view class="pb-safe" />
  84. </view>
  85. </view>
  86. </template>
  87. <style scoped lang="scss">
  88. .border-and-fixed {
  89. position: fixed;
  90. bottom: 0;
  91. left: 0;
  92. right: 0;
  93. border-top: 1px solid #eee;
  94. box-sizing: border-box;
  95. }
  96. </style>