feat 完善三级路由功能

This commit is contained in:
Asoka 2025-06-05 18:32:22 +08:00
parent 73e633f686
commit 310af266f9
4 changed files with 64 additions and 72 deletions

View File

@ -101,13 +101,11 @@ onBeforeMount(() => {
// (mittBus.off('setSendColumnsChildren))
// 使
mittBus.on('setSendColumnsChildren', (res: MittMenu) => {
console.log('setSendColumnsChildren', res)
!res.children || res.children.length < 1 ? (themeConfig.value.isCollapse = true) : (themeConfig.value.isCollapse = false)
state.menuList = res.children
})
//
mittBus.on('setSendClassicChildren', (res: MittMenu) => {
console.log('setSendClassicChildren', res)
let { layout, isClassicSplitMenu } = themeConfig.value
if (layout === 'classic' && isClassicSplitMenu) {
//

View File

@ -3,9 +3,7 @@
<div class="left-section">
<Logo v-if="setIsShowLogo" />
</div>
<Horizontal :menuList="state.menuList" />
<div class="right-section">
<User />
</div>
@ -14,11 +12,13 @@
<script setup lang="ts" name="layoutBreadcrumbIndex">
import { defineAsyncComponent, computed, reactive, onMounted, onUnmounted, onBeforeMount } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useRoute, useRouter, RouteRecordRaw } from 'vue-router'
import { storeToRefs } from 'pinia'
import { useRoutesList } from '/@/stores/routesList'
import { useThemeConfig } from '/@/stores/themeConfig'
import mittBus from '/@/utils/mitt'
import { treeToList, listToTree, filterList } from '/@/utils/tree'
import { cloneDeep } from 'lodash-es'
//
const User = defineAsyncComponent(() => import('/@/layout/navBars/topBar/user.vue'))
@ -51,11 +51,7 @@ const setIsShowLogo = computed(() => {
let { isShowLogo, layout } = themeConfig.value
return (isShowLogo && layout === 'classic') || (isShowLogo && layout === 'transverse')
})
//
const isLayoutTransverse = computed(() => {
let { layout, isClassicSplitMenu } = themeConfig.value
return layout === 'transverse' || (isClassicSplitMenu && layout === 'classic')
})
// //
const setFilterRoutes = () => {
let { layout, isClassicSplitMenu } = themeConfig.value
@ -64,18 +60,13 @@ const setFilterRoutes = () => {
//
state.menuList = filterRoutesFun(routesList.value) //
const resData = setSendClassicChildren(route.path)
mittBus.emit('setSendClassicChildren', resData)
//console.log('/', resData)
//mittBus.emit('setSendClassicChildren', resData)
} else {
state.menuList = filterRoutesFun(routesList.value)
}
}
// children
const delClassicChildren = <T extends ChilType>(arr: T[]): T[] => {
arr.map((v: T) => {
if (v.children) delete v.children
})
return arr
}
//
const filterRoutesFun = <T extends RouteItem>(arr: T[]): T[] => {
return arr
@ -87,47 +78,45 @@ const filterRoutesFun = <T extends RouteItem>(arr: T[]): T[] => {
return item
})
}
//
const getRootPath = (path: string) => {
let rootPath = ''
let routeTree = listToTree(
filterList(treeToList(cloneDeep(routesList.value)), path, {
filterWhere: (item: any, filterword: string) => {
return item.path?.toLocaleLowerCase() === filterword
},
})
)
if (routeTree.length > 0 && routeTree[0]?.path) {
rootPath = routeTree[0].path
}
return rootPath
}
//
const setSendClassicChildren = (path: string) => {
const currentPathSplit = path.split('/')
let currentData: MittMenu = { children: [] }
filterRoutesFun(routesList.value).map((v: RouteItem, k: number) => {
if (v.path === `/${currentPathSplit[1]}`) {
v['k'] = k
currentData['item'] = { ...v }
currentData['children'] = [{ ...v }]
if (v.children) currentData['children'] = v.children
let res: MittMenu = { children: [] }
//
const rootPath = getRootPath(path)
const module = state.menuList.find(item => item.path === rootPath)
if (module && module.children) {
const sub = module.children.find((child: RouteRecordRaw) => path.startsWith(child.path))
if (sub) {
res.item = { ...sub }
res.children = sub.children ? [...sub.children] : []
}
})
return currentData
}
// props.menuList fallback layout.children
const topModules = computed<RouteItem[]>(() => {
if (props.menuList && props.menuList.length) {
return filterRoutesFun(props.menuList)
}
const layout = routesList.value.find((item: RouteItem) => item.path === '/')
if (!layout || !layout.children) return []
return filterRoutesFun(layout.children)
})
const onModuleChange = (path: string) => {
router.push(path)
mittBus.emit('getBreadcrumbIndexSetFilterRoutes')
if (themeConfig.value.layout === 'classic' && themeConfig.value.isClassicSplitMenu) {
mittBus.emit('setSendClassicChildren', path)
}
return res
}
//
onMounted(() => {
setFilterRoutes()
mittBus.on('getBreadcrumbIndexSetFilterRoutes', () => {
setFilterRoutes()
})
if (themeConfig.value.layout === 'classic' && themeConfig.value.isClassicSplitMenu) {
mittBus.emit('setSendClassicChildren', route.path)
mittBus.emit('setSendClassicChildren', setSendClassicChildren(route.path))
}
})
//
@ -137,7 +126,8 @@ onUnmounted(() => {
onBeforeMount(() => {
if (themeConfig.value.layout === 'classic' && themeConfig.value.isClassicSplitMenu) {
mittBus.emit('setSendClassicChildren', route.path)
//console.log('onBeforeMount11122', route.path)
//mittBus.emit('setSendClassicChildren', route.path)
}
})

View File

@ -108,6 +108,9 @@ const currentModule = computed(() => {
state.currentModulePath === module.path ||
route.path.startsWith(module.path)
)
console.log('state.currentModulePath', state.currentModulePath)
return current || moduleList.value[0] || {
path: '',
title: '选择模块',
@ -172,10 +175,6 @@ const setCurrentRouterHighlight = (currentRoute: RouteToFrom) => {
else state.defaultActive = path
}
}
//
const onALinkClick = (val: RouteItem) => {
other.handleOpenLink(val)
}
//
const onModuleCommand = (path: string) => {
@ -232,7 +231,22 @@ const setSendSubMenuChildren = (path: string) => {
}
}
console.log('res', res)
return res
}
const setBeforeRouteUpdateClassicChildren = (path: string) => {
let res: MittMenu = { children: [] }
//
const module = menuLists.value.find(item => item.path === state.currentModulePath)
if (module && module.children) {
const sub = module.children.find((child: RouteRecordRaw) => path.startsWith(child.path))
if (sub) {
res.item = { ...sub }
res.children = sub.children ? [...sub.children] : []
}
}
return res
}
@ -240,8 +254,6 @@ const setSendSubMenuChildren = (path: string) => {
const onSubMenuClick = (path: string) => {
//
router.push(path)
//
console.log('onSubMenuClick setSendColumnsChildren4', path)
mittBus.emit('setSendColumnsChildren', setSendSubMenuChildren(path))
}
@ -270,11 +282,11 @@ const initCurrentModule = () => {
state.currentModulePath = menuLists.value[0].path
}
}
//
onBeforeRouteUpdate((to) => {
// https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
setCurrentRouterHighlight(to)
//
const matchedModule = menuLists.value.find(module =>
to.path.startsWith(module.path) && module.path !== '/'
@ -282,6 +294,11 @@ onBeforeRouteUpdate((to) => {
if (matchedModule) {
state.currentModulePath = matchedModule.path
}
console.log('onBeforeRouteUpdate', to.path, matchedModule.path)
mittBus.emit('setSendClassicChildren', setBeforeRouteUpdateClassicChildren(to.path))
// // tagsView
// let { layout, isClassicSplitMenu } = themeConfig.value
// if (layout === 'classic' && isClassicSplitMenu) {

View File

@ -70,15 +70,10 @@ const localMenuList = ref<RouteItem[]>([])
//
localMenuList.value = props.menuList
// Debug: initial menuList received
console.log('vertical initial menuList:', props.menuList)
// menuList
watch(
() => props.menuList,
(newList) => {
// Debug: props.menuList changed
console.log('vertical props.menuList changed:', newList)
localMenuList.value = newList
},
{ immediate: true }
@ -86,8 +81,6 @@ watch(
//
mittBus.on('setSendColumnsChildren', (res) => {
// Debug: received setSendColumnsChildren event
console.log('vertical received setSendColumnsChildren:', res)
localMenuList.value = res.children || []
})
@ -99,8 +92,6 @@ const getThemeConfig = computed(() => {
})
//
const setParentHighlight = (currentRoute: RouteToFrom) => {
console.log('vertical currentRoute', currentRoute)
const { path, meta } = currentRoute
const pathSplit = meta?.isDynamic ? meta.isDynamicPath!.split('/') : path!.split('/')
if (pathSplit.length >= 4 && meta?.isHide) return pathSplit.splice(0, 3).join('/')
@ -112,14 +103,10 @@ const onALinkClick = (val: RouteItem) => {
}
//
onMounted(() => {
// Debug: onMounted, current route
console.log('vertical onMounted route.path:', route.path)
state.defaultActive = setParentHighlight(route)
})
//
onBeforeRouteUpdate((to) => {
// Debug: onBeforeRouteUpdate, target route
console.log('vertical onBeforeRouteUpdate to.path:', to.path)
state.defaultActive = setParentHighlight(to)
const clientWidth = document.body.clientWidth
if (clientWidth < 1000) themeConfig.value.isCollapse = false