Skip to content

Commit

Permalink
fix🐛: 菜单 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
abulo committed Jul 1, 2024
1 parent f3583d5 commit ad2ed14
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion admin/src/layouts/LayoutClassic/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const globalStore = useGlobalStore();
const accordion = computed(() => globalStore.accordion);
const isCollapse = computed(() => globalStore.isCollapse);
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.meta.id) as string);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : String(route.meta.id)) as string);
</script>

<style scoped lang="scss">
Expand Down
19 changes: 10 additions & 9 deletions admin/src/layouts/LayoutColumns/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<script setup lang="ts" name="layoutColumns">
import { ref, computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";
import Main from "@/layouts/components/Main/index.vue";
Expand All @@ -65,27 +65,28 @@ const globalStore = useGlobalStore();
const accordion = computed(() => globalStore.accordion);
const isCollapse = computed(() => globalStore.isCollapse);
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.meta.id) as string);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : String(route.meta.id)) as string);
const subMenuList = ref<Menu.MenuOptions[]>([]);
const splitActive = ref();
const flatMenuList = computed(() => authStore.flatMenuListGet);
const getRouteItem = () => {
const name = route.name;
let routeItem: RouteLocationNormalizedLoadedGeneric = null;
let routeItemId: number = 0;
for (const item of route.matched) {
if (item.name == name) {
routeItem = item;
routeItemId = Number(item.meta.id);
break;
}
}
return routeItem as Menu.MenuOptions;
return flatMenuList.value.find(item => item.meta.id === routeItemId);
};
const getActiveHeaderMenu = () => {
const routeItem = getRouteItem();
const routeItem = getRouteItem() as Menu.MenuOptions;
const menuItem = findRootMenuByPath(authStore.authMenuList, routeItem);
return menuItem?.meta?.id || "";
return String(menuItem?.meta?.id) || "";
};
watch(
Expand All @@ -94,7 +95,7 @@ watch(
// 当前菜单没有数据直接 return
if (!menuList.value.length) return;
splitActive.value = getActiveHeaderMenu();
const routeItem = getRouteItem();
const routeItem = getRouteItem() as Menu.MenuOptions;
const menuItem = getShowMenuItem(findRootMenuByPath(authStore.authMenuList, routeItem) as Menu.MenuOptions);
if (menuItem?.children?.length) return (subMenuList.value = menuItem.children);
subMenuList.value = [];
Expand All @@ -108,7 +109,7 @@ watch(
// change SubMenu
const changeSubMenu = (item: Menu.MenuOptions) => {
let actionRouteItem = findRootMenuByPath(menuList.value, item);
splitActive.value = actionRouteItem?.meta?.id || "";
splitActive.value = String(actionRouteItem?.meta?.id) || "";
if (item?.children?.length) return (subMenuList.value = item.children);
subMenuList.value = [];
router.push(item.path);
Expand Down
17 changes: 9 additions & 8 deletions admin/src/layouts/LayoutMixed/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<script setup lang="ts" name="layoutVertical">
import { ref, computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";
import Main from "@/layouts/components/Main/index.vue";
Expand All @@ -61,32 +61,33 @@ const asideBoxWidth = computed(() => {
return isCollapse.value ? 65 : 210;
});
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.meta.id) as string);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : String(route.meta.id)) as string);
const subMenuList = ref<Menu.MenuOptions[]>([]);
const activeHeaderMenu = ref("");
const flatMenuList = computed(() => authStore.flatMenuListGet);
const getRouteItem = () => {
const name = route.name;
let routeItem: RouteLocationNormalizedLoadedGeneric = null;
let routeItemId: number = 0;
for (const item of route.matched) {
if (item.name == name) {
routeItem = item;
routeItemId = Number(item.meta.id);
break;
}
}
return routeItem as Menu.MenuOptions;
return flatMenuList.value.find(item => item.meta.id === routeItemId);
};
const getActiveHeaderMenu = () => {
const routeItem = getRouteItem();
const routeItem = getRouteItem() as Menu.MenuOptions;
const menuItem = findRootMenuByPath(authStore.authMenuList, routeItem);
return menuItem?.meta?.id || "";
return String(menuItem?.meta?.id) || "";
};
watch(
() => [menuList, route],
() => {
// 当前菜单没有数据直接 return
if (!menuList.value.length) return;
activeHeaderMenu.value = getActiveHeaderMenu();
const routeItem = getRouteItem();
const routeItem = getRouteItem() as Menu.MenuOptions;
const menuItem = getShowMenuItem(findRootMenuByPath(authStore.authMenuList, routeItem) as Menu.MenuOptions);
if (menuItem?.children?.length) return (subMenuList.value = menuItem.children);
subMenuList.value = [];
Expand Down
2 changes: 1 addition & 1 deletion admin/src/layouts/LayoutTransverse/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.meta.id) as string);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : String(route.meta.id)) as string);
const handleClickMenu = (subItem: Menu.MenuOptions) => {
if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
Expand Down
2 changes: 1 addition & 1 deletion admin/src/layouts/LayoutVertical/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const globalStore = useGlobalStore();
const accordion = computed(() => globalStore.accordion);
const isCollapse = computed(() => globalStore.isCollapse);
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.meta.id) as string);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : String(route.meta.id)) as string);
</script>

<style scoped lang="scss">
Expand Down

0 comments on commit ad2ed14

Please sign in to comment.