index.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { defineComponent, ref, watchEffect } from 'vue';
  2. import { useRouter, useRoute } from 'vue-router';
  3. import { NAV_BAR_MENUS } from '@/constants/constants';
  4. import './index.scss';
  5. export default defineComponent({
  6. setup() {
  7. const router = useRouter();
  8. const route = useRoute();
  9. const currentRouteInMenusPath = NAV_BAR_MENUS.map((m) => m.path).find((p) =>
  10. route.path.includes(p),
  11. );
  12. const defaultActive = ref(currentRouteInMenusPath);
  13. watchEffect(() => {
  14. defaultActive.value = NAV_BAR_MENUS.map((m) => m.path).find((p) =>
  15. route.path.includes(p),
  16. );
  17. });
  18. return () => (
  19. <el-menu
  20. class="nav-bar-menu"
  21. mode="horizontal"
  22. ellipsis={false}
  23. defaultActive={defaultActive.value}>
  24. {NAV_BAR_MENUS.map((m) => (
  25. <el-menuItem
  26. index={m.path}
  27. onClick={() => {
  28. defaultActive.value = m.path;
  29. router.push(m.path);
  30. }}>
  31. <m.icon
  32. width="16px"
  33. height="16px"
  34. style={{ marginRight: '10px' }}
  35. />
  36. {m.name}
  37. </el-menuItem>
  38. ))}
  39. </el-menu>
  40. );
  41. },
  42. });