|
|
@@ -33,7 +33,7 @@
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts" name="layoutVertical">
|
|
|
-import { computed, ref, watch, nextTick } from "vue";
|
|
|
+import { computed, ref } from "vue";
|
|
|
import { useRoute } from "vue-router";
|
|
|
import { useAuthStore } from "@/stores/modules/auth";
|
|
|
import { useGlobalStore } from "@/stores/modules/global";
|
|
|
@@ -42,6 +42,7 @@ import ToolBarLeft from "@/layouts/components/Header/ToolBarLeft.vue";
|
|
|
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
|
|
|
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
|
|
|
import type { ElMenu } from "element-plus";
|
|
|
+import { useOpenedMenus } from "@/layouts/hooks/useOpenedMenus";
|
|
|
|
|
|
const title = import.meta.env.VITE_GLOB_APP_TITLE;
|
|
|
|
|
|
@@ -54,54 +55,7 @@ const menuList = computed(() => authStore.showMenuListGet);
|
|
|
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string);
|
|
|
|
|
|
const menuRef = ref<InstanceType<typeof ElMenu>>();
|
|
|
-const openedMenus = ref<string[]>([]);
|
|
|
-
|
|
|
-// 计算应该展开的父菜单路径
|
|
|
-const getParentMenuPath = (path: string) => {
|
|
|
- const pathSegments = path.split("/").filter(Boolean);
|
|
|
- // 只有当路径有两段或以上时才返回父菜单路径
|
|
|
- // 例如: /licenseManagement/businessLicense -> /licenseManagement
|
|
|
- if (pathSegments.length > 1) {
|
|
|
- return `/${pathSegments[0]}`;
|
|
|
- }
|
|
|
- return "";
|
|
|
-};
|
|
|
-
|
|
|
-// 根据当前路由计算应该展开的父菜单
|
|
|
-const calculateOpenedMenus = () => {
|
|
|
- const currentPath = (route.meta.activeMenu as string) || route.path;
|
|
|
- const parentPath = getParentMenuPath(currentPath);
|
|
|
-
|
|
|
- if (parentPath) {
|
|
|
- // 如果手风琴模式开启,只保留当前父菜单
|
|
|
- if (accordion.value) {
|
|
|
- return [parentPath];
|
|
|
- } else {
|
|
|
- // 如果手风琴模式关闭,保留已有的展开菜单,并添加当前父菜单
|
|
|
- const newOpenedMenus = [...openedMenus.value];
|
|
|
- if (!newOpenedMenus.includes(parentPath)) {
|
|
|
- newOpenedMenus.push(parentPath);
|
|
|
- }
|
|
|
- return newOpenedMenus;
|
|
|
- }
|
|
|
- }
|
|
|
- return openedMenus.value;
|
|
|
-};
|
|
|
-
|
|
|
-// 监听路由变化,保持父菜单展开
|
|
|
-watch(
|
|
|
- () => [route.path, route.meta.activeMenu],
|
|
|
- () => {
|
|
|
- nextTick(() => {
|
|
|
- const newOpenedMenus = calculateOpenedMenus();
|
|
|
- // 只有当展开菜单发生变化时才更新
|
|
|
- if (JSON.stringify(newOpenedMenus.sort()) !== JSON.stringify(openedMenus.value.sort())) {
|
|
|
- openedMenus.value = newOpenedMenus;
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- { immediate: true }
|
|
|
-);
|
|
|
+const { openedMenus } = useOpenedMenus({ route, accordion });
|
|
|
</script>
|
|
|
|
|
|
<style scoped lang="scss">
|