perf: 切换tab时自动展开对应的菜单

This commit is contained in:
zclzone 2022-11-25 09:16:01 +08:00
parent 264119a142
commit ed79e81b13

View File

@ -1,12 +1,13 @@
<template> <template>
<n-menu <n-menu
ref="menu"
class="side-menu" class="side-menu"
accordion accordion
:indent="18" :indent="18"
:collapsed-icon-size="22" :collapsed-icon-size="22"
:collapsed-width="64" :collapsed-width="64"
:options="menuOptions" :options="menuOptions"
:value="curRoute.meta?.activeMenu || curRoute.name" :value="activeKey"
@update:value="handleMenuSelect" @update:value="handleMenuSelect"
/> />
</template> </template>
@ -20,10 +21,18 @@ const curRoute = useRoute()
const permissionStore = usePermissionStore() const permissionStore = usePermissionStore()
const appStore = useAppStore() const appStore = useAppStore()
const activeKey = computed(() => curRoute.meta?.activeMenu || curRoute.name)
const menuOptions = computed(() => { const menuOptions = computed(() => {
return permissionStore.menus.map((item) => getMenuItem(item)).sort((a, b) => a.order - b.order) return permissionStore.menus.map((item) => getMenuItem(item)).sort((a, b) => a.order - b.order)
}) })
const menu = ref(null)
watch(curRoute, async () => {
await nextTick()
menu.value?.showOption()
})
function resolvePath(basePath, path) { function resolvePath(basePath, path) {
if (isExternal(path)) return path if (isExternal(path)) return path
return ( return (
@ -52,11 +61,11 @@ function getMenuItem(route, basePath = '') {
// //
const singleRoute = visibleChildren[0] const singleRoute = visibleChildren[0]
menuItem = { menuItem = {
...menuItem,
label: singleRoute.meta?.title || singleRoute.name, label: singleRoute.meta?.title || singleRoute.name,
key: singleRoute.name, key: singleRoute.name,
path: resolvePath(menuItem.path, singleRoute.path), path: resolvePath(menuItem.path, singleRoute.path),
icon: getIcon(singleRoute.meta), icon: getIcon(singleRoute.meta),
order: menuItem.order,
} }
const visibleItems = singleRoute.children ? singleRoute.children.filter((item) => item.name && !item.isHidden) : [] const visibleItems = singleRoute.children ? singleRoute.children.filter((item) => item.name && !item.isHidden) : []
@ -70,7 +79,6 @@ function getMenuItem(route, basePath = '') {
.map((item) => getMenuItem(item, menuItem.path)) .map((item) => getMenuItem(item, menuItem.path))
.sort((a, b) => a.order - b.order) .sort((a, b) => a.order - b.order)
} }
return menuItem return menuItem
} }