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> </template>
<script setup lang="ts"> <script setup lang="ts">
import { nextTick, reactive, watch } from 'vue' import { nextTick, reactive } from 'vue'
import { editDefaultLang } from '/@/lang/index' import { editDefaultLang } from '/@/lang/index'
import { useConfig } from '/@/stores/config' import { useConfig } from '/@/stores/config'
import { useUserInfo } from '/@/stores/userInfo' import { useUserInfo } from '/@/stores/userInfo'
@ -78,7 +78,7 @@ import { fullUrl } from '/@/utils/common'
import MenuSub from '/@/layouts/frontend/components/menuSub.vue' import MenuSub from '/@/layouts/frontend/components/menuSub.vue'
import toggleDark from '/@/utils/useDark' import toggleDark from '/@/utils/useDark'
import DarkSwitch from '/@/layouts/common/components/darkSwitch.vue' 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 type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
import { layoutMenuRef } from '/@/stores/refs' import { layoutMenuRef } from '/@/stores/refs'
@ -101,37 +101,25 @@ const state = reactive({
activeMenu: '', 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) => { const setActiveMenu = (route: RouteLocationNormalizedLoaded) => {
if (route.path == '/') return (state.activeMenu = 'index') if (route.path == '/') return (state.activeMenu = 'index')
// const menuId = findMenus(route)
const menuId = findMenu(route)
if (menuId) { if (menuId) {
state.activeMenu = 'column-' + menuId state.activeMenu = 'column-' + menuId
} else if (route.path.startsWith('/user')) { } else if (route.path.startsWith('/user')) {
state.activeMenu = 'user' state.activeMenu = 'user'
} }
} }
setActiveMenu(route)
const onSelect = (index: string) => {
/** /**
* 当动态菜单被点击时 * 菜单被点击时额外对无需激活的菜单处理外链暗黑模式开关语言切换等
* 检查菜单是否需要激活如果否还原 state.activeMenu * 检查菜单是否需要激活如果否还原 state.activeMenu
*/ */
const onSelect = (index: string) => {
if (noNeedActive(siteConfig.headNav, index) || noNeedActive(memberCenter.state.navUserMenus, index)) { if (noNeedActive(siteConfig.headNav, index) || noNeedActive(memberCenter.state.navUserMenus, index)) {
const oldActiveMenu = state.activeMenu const oldActiveMenu = state.activeMenu
state.activeMenu = '' state.activeMenu = ''
@ -171,12 +159,43 @@ const isExternalLink = (menus: RouteRecordRaw[], index: string): boolean => {
return false return false
} }
watch( /**
() => route.path, * 递归的搜索菜单 Index
() => { */
setActiveMenu(route) 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> </script>
<style scoped lang="scss"> <style scoped lang="scss">