NavItem.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 route = useRoute();
  18. const target = props.to;
  19. const isChatType = (cur_path) => {
  20. return cur_path === '/chat' || cur_path === '/chat/history' || cur_path === '/chat/board' || cur_path === '/chat/qa';
  21. };
  22. const is_active = computed(() => {
  23. const cur_path = route.path;
  24. if (target === '/chat/history') {
  25. // 对话
  26. const is_chat = isChatType(cur_path);
  27. if (is_chat) {
  28. return true;
  29. } else {
  30. return false;
  31. }
  32. }
  33. return cur_path === target;
  34. });
  35. const img_src = computed(() => {
  36. if (is_active.value) {
  37. // TODO: 如果是激活状态,应该改为 2
  38. // 资源目录应该添加名为 2 的图标
  39. return `../../assets/${props.icon_name}-2.png`;
  40. } else {
  41. return `../../assets/${props.icon_name}-1.png`;
  42. }
  43. });
  44. </script>
  45. <style lang="css" scoped>
  46. .nav-item {
  47. display: flex;
  48. justify-content: center;
  49. align-items: center;
  50. height: 40px;
  51. border-radius: 20px;
  52. padding-left: 15px;
  53. padding-right: 15px;
  54. margin-left: 5px;
  55. margin-right: 5px;
  56. color: var(--color-text-1);
  57. }
  58. .nav-item:hover {
  59. background-color: var(--color-fill-4);
  60. color: #fff;
  61. }
  62. .nav-item.is-active {
  63. background-color: rgb(var(--primary-6));
  64. color: #fff;
  65. }
  66. .nav-item-icon {
  67. display: flex;
  68. justify-content: center;
  69. align-items: center;
  70. margin-right: 5px;
  71. }
  72. .nav-item-icon img {
  73. width: 18px;
  74. }
  75. </style>