YTabBar.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <van-tabbar v-model="activeTab" :fixed="fixed" :placeholder="placeholder" :safe-area-inset-bottom="safeArea" route>
  3. <van-tabbar-item
  4. v-for="item in tabList"
  5. :key="item.path"
  6. :to="item.path"
  7. :icon="item.icon"
  8. :badge="item.badge"
  9. :dot="item.dot"
  10. >
  11. {{ $t(item.title) }}
  12. </van-tabbar-item>
  13. </van-tabbar>
  14. </template>
  15. <script setup lang="ts">
  16. import { ref, watch, computed } from 'vue'
  17. import { useRoute } from 'vue-router'
  18. import { useAppStore } from '@/store/modules/app'
  19. const props = defineProps({
  20. fixed: {
  21. type: Boolean,
  22. default: true
  23. },
  24. placeholder: {
  25. type: Boolean,
  26. default: true
  27. },
  28. safeArea: {
  29. type: Boolean,
  30. default: true
  31. }
  32. })
  33. const route = useRoute()
  34. const appStore = useAppStore()
  35. const activeTab = ref(0)
  36. // Tab列表配置
  37. const tabList = computed(() => {
  38. // 基础 Tab 配置
  39. const tabs = [
  40. {
  41. path: '/index',
  42. icon: 'home-o',
  43. title: 'index.home'
  44. },
  45. {
  46. path: '/menu',
  47. icon: 'apps-o',
  48. title: 'menu.title'
  49. },
  50. {
  51. path: '/order',
  52. icon: 'orders-o',
  53. title: 'order.title'
  54. },
  55. {
  56. path: '/mine',
  57. icon: 'user-o',
  58. title: 'mine.title'
  59. }
  60. ]
  61. // 如果在店铺模式或桌台模式下,首页图标应指向该店铺的菜单页/首页
  62. if (!appStore.isPlatformMode && appStore.currentShop?.id) {
  63. // 构造带有查询参数的路径
  64. let shopHomePath = `/menu?shopId=${appStore.currentShop.id}`
  65. // 如果有桌台信息,也带上
  66. if (appStore.currentTable?.code) {
  67. shopHomePath += `&tableCode=${appStore.currentTable.code}`
  68. }
  69. // 替换第一个 Tab (首页) 的路径
  70. tabs[0].path = shopHomePath
  71. // 可选:如果希望"菜单" Tab 也指向同一个地方(或者隐藏它),
  72. // 这里保持原状,因为 /menu 本身会读取 store 中的 shopId
  73. }
  74. return tabs
  75. })
  76. // 监听路由变化更新active
  77. watch(
  78. () => route.path,
  79. (newPath) => {
  80. // 简单匹配 path,忽略 query
  81. const index = tabList.value.findIndex((item) => {
  82. // 处理带参数的 path 比较
  83. const itemPathBase = item.path.split('?')[0]
  84. return itemPathBase === newPath
  85. })
  86. if (index !== -1) {
  87. activeTab.value = index
  88. }
  89. },
  90. { immediate: true }
  91. )
  92. </script>
  93. <style scoped>
  94. /* 自定义样式可在这里添加 */
  95. </style>