NavItem.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <router-link :to="to">
  3. <div class="nav-item" :class="{ 'is-active': is_active }">
  4. <span class="nav-item-icon"><img :src="img_src" /></span>
  5. <span>{{ label }}</span>
  6. </div>
  7. </router-link>
  8. </template>
  9. <script setup>
  10. import { ref, computed } from 'vue';
  11. import { useRoute } from 'vue-router';
  12. const props = defineProps({
  13. label: String,
  14. to: String,
  15. icon_name: String
  16. });
  17. const is_active = computed(() => {
  18. const route = useRoute();
  19. const cur_path = route.path;
  20. return cur_path === props.to;
  21. });
  22. const img_src = computed(() => {
  23. if (is_active.value) {
  24. // TODO: 如果是激活状态,应该改为 2
  25. // 资源目录应该添加名为 2 的图标
  26. return `../../assets/${props.icon_name}-1.png`;
  27. } else {
  28. return `../../assets/${props.icon_name}-1.png`;
  29. }
  30. });
  31. </script>
  32. <style lang="css" scoped>
  33. .nav-item {
  34. display: flex;
  35. justify-content: center;
  36. align-items: center;
  37. height: 40px;
  38. border-radius: 20px;
  39. padding-left: 15px;
  40. padding-right: 15px;
  41. margin-left: 5px;
  42. margin-right: 5px;
  43. color: var(--color-text-1);
  44. }
  45. .nav-item:hover {
  46. background-color: var(--color-fill-4);
  47. color: #fff;
  48. }
  49. .nav-item.is-active {
  50. background-color: rgb(var(--primary-6));
  51. color: #fff;
  52. }
  53. .nav-item-icon {
  54. display: flex;
  55. justify-content: center;
  56. align-items: center;
  57. margin-right: 5px;
  58. }
  59. .nav-item-icon img {
  60. width: 18px;
  61. }
  62. </style>