index.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <div class="tabbar" :class="{ hide: !visible }">
  3. <div
  4. v-for="(item, index) in LIST"
  5. :key="item.label"
  6. class="tabbar-item"
  7. :class="{ active: index === activeIndex }"
  8. @click="
  9. $router.push({
  10. name: Array.isArray(item.routeName)
  11. ? item.routeName[0]
  12. : item.routeName,
  13. })
  14. "
  15. >
  16. <img :src="index === activeIndex ? item.activeIcon : item.icon" />
  17. <p>{{ item.label }}</p>
  18. </div>
  19. </div>
  20. </template>
  21. <script setup>
  22. import { computed } from "vue";
  23. import { useRoute } from "vue-router";
  24. import HomeIcon from "./images/icon_home_normal@2x-min.png";
  25. import HomeActiveIcon from "./images/icon_home_active@2x-min.png";
  26. import BookIcon from "./images/icon_book_normal@2x-min.png";
  27. import BookActiveIcon from "./images/icon_book_active@2x-min.png";
  28. import UserIcon from "./images/icon_user_normal@2x-min.png";
  29. import UserActiveIcon from "./images/icon_user_active@2x-min.png";
  30. defineProps({
  31. visible: {
  32. type: Boolean,
  33. default: false,
  34. },
  35. });
  36. const LIST = [
  37. {
  38. label: "首页",
  39. icon: HomeIcon,
  40. activeIcon: HomeActiveIcon,
  41. routeName: ["home", "home2"],
  42. },
  43. {
  44. label: "书库",
  45. icon: BookIcon,
  46. activeIcon: BookActiveIcon,
  47. routeName: "stack",
  48. },
  49. {
  50. label: "我的",
  51. icon: UserIcon,
  52. activeIcon: UserActiveIcon,
  53. routeName: "mine",
  54. },
  55. ];
  56. const route = useRoute();
  57. const activeIndex = computed(() => {
  58. return LIST.findIndex((item) =>
  59. Array.isArray(item.routeName)
  60. ? item.routeName.includes(route.name)
  61. : item.routeName === route.name
  62. );
  63. });
  64. </script>
  65. <style lang="scss" scoped>
  66. @import "./index.scss";
  67. </style>