vertical.vue 2.6 KB

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