You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

  1. <script setup lang="ts">
  2. import Logo from "./logo.vue";
  3. import { emitter } from "/@/utils/mitt";
  4. import { useNav } from "../../hooks/nav";
  5. import SidebarItem from "./sidebarItem.vue";
  6. import { storageLocal } from "/@/utils/storage";
  7. import { useRoute, useRouter } from "vue-router";
  8. import { ref, computed, watch, onBeforeMount } from "vue";
  9. import { findRouteByPath, getParentPaths } from "/@/router/utils";
  10. import { usePermissionStoreHook } from "/@/store/modules/permission";
  11. const route = useRoute();
  12. const routers = useRouter().options.routes;
  13. const showLogo = ref(
  14. storageLocal.getItem("responsive-configure")?.showLogo ?? true
  15. );
  16. const { pureApp, isCollapse, menuSelect } = useNav();
  17. let subMenuData = ref([]);
  18. const menuData = computed(() => {
  19. return pureApp.layout === "mix"
  20. ? subMenuData.value
  21. : usePermissionStoreHook().wholeMenus;
  22. });
  23. function getSubMenuData(path) {
  24. // path的上级路由组成的数组
  25. const parentPathArr = getParentPaths(
  26. path,
  27. usePermissionStoreHook().wholeMenus
  28. );
  29. // 当前路由的父级路由信息
  30. const parenetRoute = findRouteByPath(
  31. parentPathArr[0] || path,
  32. usePermissionStoreHook().wholeMenus
  33. );
  34. if (!parenetRoute?.children) return;
  35. subMenuData.value = parenetRoute?.children;
  36. }
  37. getSubMenuData(route.path);
  38. onBeforeMount(() => {
  39. emitter.on("logoChange", key => {
  40. showLogo.value = key;
  41. });
  42. });
  43. watch(
  44. () => route.path,
  45. () => {
  46. getSubMenuData(route.path);
  47. }
  48. );
  49. </script>
  50. <template>
  51. <div :class="['sidebar-container', showLogo ? 'has-logo' : '']">
  52. <Logo v-if="showLogo" :collapse="isCollapse" />
  53. <el-scrollbar wrap-class="scrollbar-wrapper">
  54. <el-menu
  55. :default-active="route.path"
  56. :collapse="isCollapse"
  57. unique-opened
  58. router
  59. :collapse-transition="false"
  60. mode="vertical"
  61. class="outer-most"
  62. @select="indexPath => menuSelect(indexPath, routers)"
  63. >
  64. <sidebar-item
  65. v-for="routes in menuData"
  66. :key="routes.path"
  67. :item="routes"
  68. class="outer-most"
  69. :base-path="routes.path"
  70. />
  71. </el-menu>
  72. </el-scrollbar>
  73. </div>
  74. </template>