refactor:优化顶栏子级菜单的激活逻辑

This commit is contained in:
妙码生花 2024-11-09 16:48:45 +08:00
parent 49a4e83a52
commit 54ed4ea1cf

View File

@ -68,7 +68,7 @@
</template>
<script setup lang="ts">
import { nextTick, reactive, watch } from 'vue'
import { nextTick, reactive } from 'vue'
import { editDefaultLang } from '/@/lang/index'
import { useConfig } from '/@/stores/config'
import { useUserInfo } from '/@/stores/userInfo'
@ -78,7 +78,7 @@ import { fullUrl } from '/@/utils/common'
import MenuSub from '/@/layouts/frontend/components/menuSub.vue'
import toggleDark from '/@/utils/useDark'
import DarkSwitch from '/@/layouts/common/components/darkSwitch.vue'
import { useRoute, useRouter } from 'vue-router'
import { onBeforeRouteUpdate, useRoute, useRouter } from 'vue-router'
import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
import { layoutMenuRef } from '/@/stores/refs'
@ -101,37 +101,25 @@ const state = reactive({
activeMenu: '',
})
const findMenu = (route: RouteLocationNormalizedLoaded) => {
for (const key in memberCenter.state.navUserMenus) {
if (memberCenter.state.navUserMenus[key].path == route.path || memberCenter.state.navUserMenus[key].name == route.name) {
return memberCenter.state.navUserMenus[key].meta?.id
}
}
for (const key in siteConfig.headNav) {
if (siteConfig.headNav[key].path == route.path || siteConfig.headNav[key].name == route.name) {
return siteConfig.headNav[key].meta?.id
}
}
}
/**
* 设置激活菜单
*/
const setActiveMenu = (route: RouteLocationNormalizedLoaded) => {
if (route.path == '/') return (state.activeMenu = 'index')
//
const menuId = findMenu(route)
const menuId = findMenus(route)
if (menuId) {
state.activeMenu = 'column-' + menuId
} else if (route.path.startsWith('/user')) {
state.activeMenu = 'user'
}
}
setActiveMenu(route)
/**
* 菜单被点击时额外对无需激活的菜单处理外链暗黑模式开关语言切换等
* 检查菜单是否需要激活如果否还原 state.activeMenu
*/
const onSelect = (index: string) => {
/**
* 当动态菜单被点击时
* 检查该菜单是否需要激活如果否还原 state.activeMenu
*/
if (noNeedActive(siteConfig.headNav, index) || noNeedActive(memberCenter.state.navUserMenus, index)) {
const oldActiveMenu = state.activeMenu
state.activeMenu = ''
@ -171,12 +159,43 @@ const isExternalLink = (menus: RouteRecordRaw[], index: string): boolean => {
return false
}
watch(
() => route.path,
() => {
setActiveMenu(route)
/**
* 递归的搜索菜单 Index
*/
const searchMenuIndex = (menus: RouteRecordRaw[], route: RouteLocationNormalizedLoaded): number | false => {
let find: boolean | number = false
for (const key in menus) {
if (menus[key].meta?.id && (menus[key].path == route.fullPath || menus[key].name == route.name)) {
return menus[key].meta.id as number
}
if (menus[key].children && menus[key].children?.length) {
find = searchMenuIndex(menus[key].children, route)
if (find !== false) return find
}
}
)
return find
}
/**
* 从动态菜单顶栏会员中心下拉会员中心菜单中搜索一个菜单
*/
const findMenus = (route: RouteLocationNormalizedLoaded) => {
//
const headNavIndex = searchMenuIndex(siteConfig.headNav, route)
if (headNavIndex !== false) return headNavIndex
//
const navUserMenuIndex = searchMenuIndex(memberCenter.state.navUserMenus, route)
if (navUserMenuIndex !== false) return navUserMenuIndex
//
return searchMenuIndex(memberCenter.state.viewRoutes, route)
}
setActiveMenu(route)
onBeforeRouteUpdate((to) => {
setActiveMenu(to)
})
</script>
<style scoped lang="scss">