vertical.vue 2.3 KB

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