vertical.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <script setup lang="ts">
  2. import Logo from "./logo.vue";
  3. import { emitter } from "/@/utils/mitt";
  4. import SidebarItem from "./sidebarItem.vue";
  5. import { algorithm } from "/@/utils/algorithm";
  6. import { storageLocal } from "/@/utils/storage";
  7. import { useRoute, useRouter } from "vue-router";
  8. import { computed, ref, onBeforeMount } from "vue";
  9. import { useAppStoreHook } from "/@/store/modules/app";
  10. import { usePermissionStoreHook } from "/@/store/modules/permission";
  11. const route = useRoute();
  12. const pureApp = useAppStoreHook();
  13. const router = useRouter().options.routes;
  14. const showLogo = ref(
  15. storageLocal.getItem("responsive-configure")?.showLogo ?? true
  16. );
  17. const isCollapse = computed(() => {
  18. return !pureApp.getSidebarStatus;
  19. });
  20. const activeMenu = computed((): string => {
  21. const { meta, path } = route;
  22. if (meta.activeMenu) {
  23. // @ts-ignore
  24. return meta.activeMenu;
  25. }
  26. return path;
  27. });
  28. const menuSelect = (indexPath: string): void => {
  29. let parentPath = "";
  30. let parentPathIndex = indexPath.lastIndexOf("/");
  31. if (parentPathIndex > 0) {
  32. parentPath = indexPath.slice(0, parentPathIndex);
  33. }
  34. // 找到当前路由的信息
  35. // eslint-disable-next-line no-inner-declarations
  36. function findCurrentRoute(routes) {
  37. return routes.map(item => {
  38. if (item.path === indexPath) {
  39. // 切换左侧菜单 通知标签页
  40. emitter.emit("changLayoutRoute", {
  41. indexPath,
  42. parentPath
  43. });
  44. } else {
  45. if (item.children) findCurrentRoute(item.children);
  46. }
  47. });
  48. }
  49. findCurrentRoute(algorithm.increaseIndexes(router));
  50. };
  51. onBeforeMount(() => {
  52. emitter.on("logoChange", key => {
  53. showLogo.value = key;
  54. });
  55. });
  56. </script>
  57. <template>
  58. <div :class="['sidebar-container', showLogo ? 'has-logo' : '']">
  59. <Logo v-if="showLogo" :collapse="isCollapse" />
  60. <el-scrollbar wrap-class="scrollbar-wrapper">
  61. <el-menu
  62. :default-active="activeMenu"
  63. :collapse="isCollapse"
  64. unique-opened
  65. router
  66. :collapse-transition="false"
  67. mode="vertical"
  68. class="outer-most"
  69. @select="menuSelect"
  70. >
  71. <sidebar-item
  72. v-for="route in usePermissionStoreHook().wholeMenus"
  73. :key="route.path"
  74. :item="route"
  75. class="outer-most"
  76. :base-path="route.path"
  77. />
  78. </el-menu>
  79. </el-scrollbar>
  80. </div>
  81. </template>