TabBar.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <script setup lang="ts">
  2. import home from '@/static/image/tab-bar/home.png'
  3. import homeSelect from '@/static/image/tab-bar/home-select.png'
  4. import manual from '@/static/image/tab-bar/manual.png'
  5. import manualSelect from '@/static/image/tab-bar/manual-select.png'
  6. import my from '@/static/image/tab-bar/me.png'
  7. import mySelect from '@/static/image/tab-bar/me-select.png'
  8. import plan from '@/static/image/tab-bar/plan.png'
  9. import planSelect from '@/static/image/tab-bar/plan-select.png'
  10. interface BottomItem {
  11. name: string
  12. icon: string
  13. iconSelect: string
  14. url: string
  15. }
  16. const tabList: Ref<BottomItem[]> = ref([
  17. { name: '首页', icon: home, iconSelect: homeSelect, url: 'pages/home/index' },
  18. { name: '喂养计划', icon: plan, iconSelect: planSelect, url: 'pages/feed-plan/index' },
  19. { name: '养宠手册', icon: manual, iconSelect: manualSelect, url: 'pages/pet-manual/index' },
  20. { name: '我的', icon: my, iconSelect: mySelect, url: 'pages/me/index' },
  21. ])
  22. function handleTap(item: BottomItem) {
  23. uni.reLaunch({ url: `/${item.url}` })
  24. }
  25. function getCurrentPageUrl() {
  26. const pages = getCurrentPages() // Get the current pages stack
  27. const currentPage = pages[pages.length - 1] // Get the current page
  28. return currentPage.route // Return the route of the current page
  29. }
  30. const selectedIndex = computed(() => {
  31. return tabList.value.findIndex(item => item.url === getCurrentPageUrl())
  32. })
  33. </script>
  34. <template>
  35. <view class="w-full p-2 fixed bottom-0 left-0 safe-area">
  36. <view class="w-full flex gap-2">
  37. <view v-for="(item, index) in tabList" :key="index" class="flex flex-col items-center justify-center w-1/4" @tap="handleTap(item)">
  38. <image :src="selectedIndex === index ? item.iconSelect : item.icon" class="w-8 h-8" />
  39. <text
  40. class="text-3 mt-1"
  41. :class="selectedIndex === index ? 'text-primary' : 'text-default'"
  42. >
  43. {{ item.name }}
  44. </text>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <style scoped>
  50. .safe-area{
  51. padding-bottom: env(safe-area-inset-bottom);
  52. }
  53. </style>