index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <div :class="{'has-logo': showLogo}">
  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. :collapse-transition="false"
  10. mode="vertical"
  11. @select="menuSelect"
  12. >
  13. <sidebar-item
  14. v-for="route in routes"
  15. :key="route.path"
  16. :item="route"
  17. :base-path="route.path"
  18. />
  19. </el-menu>
  20. </el-scrollbar>
  21. </div>
  22. </template>
  23. <script lang="ts">
  24. import {
  25. computed,
  26. defineComponent,
  27. ref,
  28. unref,
  29. nextTick,
  30. onBeforeMount
  31. } from "vue";
  32. import { useRoute, useRouter } from "vue-router";
  33. import { useStore } from "vuex";
  34. import SidebarItem from "./SidebarItem.vue";
  35. import { algorithm } from "../../../utils/algorithm";
  36. import { useDynamicRoutesHook } from "../tag/tagsHook";
  37. import { emitter } from "/@/utils/mitt";
  38. import Logo from "./Logo.vue";
  39. import { storageLocal } from "/@/utils/storage";
  40. export default defineComponent({
  41. name: "sidebar",
  42. components: { SidebarItem, Logo },
  43. setup() {
  44. const router = useRouter().options.routes;
  45. const store = useStore();
  46. const route = useRoute();
  47. const showLogo = ref(storageLocal.getItem("logoVal") || "1");
  48. const activeMenu = computed(() => {
  49. const { meta, path } = route;
  50. if (meta.activeMenu) {
  51. return meta.activeMenu;
  52. }
  53. return path;
  54. });
  55. const { dynamicRouteTags } = useDynamicRoutesHook();
  56. const menuSelect = (indexPath: string): void => {
  57. let parentPath = "";
  58. let parentPathIndex = indexPath.lastIndexOf("/");
  59. if (parentPathIndex > 0) {
  60. parentPath = indexPath.slice(0, parentPathIndex);
  61. }
  62. // 找到当前路由的信息
  63. function findCurrentRoute(routes) {
  64. return routes.map((item, key) => {
  65. if (item.path === indexPath) {
  66. dynamicRouteTags(indexPath, parentPath, item);
  67. } else {
  68. if (item.children) findCurrentRoute(item.children);
  69. }
  70. });
  71. return;
  72. }
  73. findCurrentRoute(algorithm.increaseIndexes(router));
  74. emitter.emit("changLayoutRoute", indexPath);
  75. };
  76. onBeforeMount(() => {
  77. emitter.on("logoChange", key => {
  78. showLogo.value = key;
  79. });
  80. });
  81. return {
  82. routes: computed(() => algorithm.increaseIndexes(router)),
  83. activeMenu,
  84. isCollapse: computed(() => !store.getters.sidebar.opened),
  85. menuSelect,
  86. showLogo
  87. };
  88. }
  89. });
  90. </script>