fg-tabbar.vue 2.1 KB

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