Browse Source

feat: initialize expanded menus based on current route

- Add a function to automatically expand menu items based on the current route, enhancing user navigation experience.
- Call the new function during the component's mounted lifecycle to ensure menus are correctly displayed on load.
yb 2 weeks ago
parent
commit
f6b19e2d18
1 changed files with 12 additions and 0 deletions
  1. 12 0
      src/layout/index.vue

+ 12 - 0
src/layout/index.vue

@@ -577,9 +577,21 @@ async function handleLogout() {
   router.push('/login')
 }
 
+// 根据当前路由自动展开菜单
+function initExpandedMenus() {
+  menuItems.forEach((item) => {
+    if (item.children && item.children.some((child) => route.path.startsWith(child.path))) {
+      if (!expandedMenus.value.includes(item.path)) {
+        expandedMenus.value.push(item.path)
+      }
+    }
+  })
+}
+
 onMounted(() => {
   userStore.getUserInfo()
   checkMobile()
+  initExpandedMenus()
   window.addEventListener('resize', checkMobile)
   document.addEventListener('click', closeUserMenu)
 })