fg-tabbar.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script setup lang="ts">
  2. import { tabbarStore } from './tabbar'
  3. // 'i-carbon-code',
  4. import { tabbarList as _tabBarList, cacheTabbarEnable, selectedTabbarStrategy, TABBAR_MAP } from './tabbarList'
  5. const customTabbarEnable
  6. = selectedTabbarStrategy === TABBAR_MAP.CUSTOM_TABBAR_WITH_CACHE
  7. || selectedTabbarStrategy === TABBAR_MAP.CUSTOM_TABBAR_WITHOUT_CACHE
  8. /** tabbarList 里面的 path 从 pages.config.ts 得到 */
  9. const tabbarList = _tabBarList.map(item => ({ ...item, path: `/${item.pagePath}` }))
  10. function selectTabBar({ value: index }: { value: number }) {
  11. const url = tabbarList[index].path
  12. tabbarStore.setCurIdx(index)
  13. if (cacheTabbarEnable) {
  14. uni.switchTab({ url })
  15. }
  16. else {
  17. uni.navigateTo({ url })
  18. }
  19. }
  20. onLoad(() => {
  21. // 解决原生 tabBar 未隐藏导致有2个 tabBar 的问题
  22. const hideRedundantTabbarEnable = selectedTabbarStrategy === TABBAR_MAP.CUSTOM_TABBAR_WITH_CACHE
  23. hideRedundantTabbarEnable
  24. && uni.hideTabBar({
  25. fail(err) {
  26. console.log('hideTabBar fail: ', err)
  27. },
  28. success(res) {
  29. console.log('hideTabBar success: ', res)
  30. },
  31. })
  32. })
  33. </script>
  34. <template>
  35. <wd-tabbar
  36. v-if="customTabbarEnable"
  37. v-model="tabbarStore.curIdx"
  38. bordered
  39. safe-area-inset-bottom
  40. placeholder
  41. fixed
  42. @change="selectTabBar"
  43. >
  44. <block v-for="(item, idx) in tabbarList" :key="item.path">
  45. <wd-tabbar-item v-if="item.iconType === 'uiLib'" :title="item.text" :icon="item.icon" />
  46. <wd-tabbar-item
  47. v-else-if="item.iconType === 'unocss' || item.iconType === 'iconfont'"
  48. :title="item.text"
  49. >
  50. <template #icon>
  51. <view
  52. h-40rpx
  53. w-40rpx
  54. :class="[item.icon, idx === tabbarStore.curIdx ? 'is-active' : 'is-inactive']"
  55. />
  56. </template>
  57. </wd-tabbar-item>
  58. <wd-tabbar-item v-else-if="item.iconType === 'local'" :title="item.text">
  59. <template #icon>
  60. <image :src="item.icon" h-40rpx w-40rpx />
  61. </template>
  62. </wd-tabbar-item>
  63. </block>
  64. </wd-tabbar>
  65. </template>