Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hxyj-admin-fe
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hxyj
hxyj-admin-fe
Commits
44cb8fff
Commit
44cb8fff
authored
Jun 10, 2025
by
tyyin lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 路由配置
parent
09e28fcd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
24 deletions
+34
-24
sidebar.vue
src/layout/components/sidebar/sidebar.vue
+15
-8
index.vue
src/layout/index.vue
+5
-3
utils.ts
src/router/utils.ts
+12
-12
router.d.ts
types/router.d.ts
+2
-1
No files found.
src/layout/components/sidebar/sidebar.vue
View file @
44cb8fff
...
...
@@ -2,21 +2,22 @@
import
{
sidebarMenus
}
from
'@/router/index'
import
{
useDesignSettingStore
}
from
'@/store/modules/design-setting'
import
SidebarLogo
from
'../sidebar-logo/sidebar-logo.vue'
//
import { type MenuOption } from '@/router/utils'
import
{
type
MenuOption
}
from
'@/router/utils'
import
{
useRoute
,
useRouter
}
from
'vue-router'
import
{
ref
,
watch
}
from
'vue'
import
EllipsisTooltipText
from
'@/components/ellipsis-tooltip-text.vue'
import
{
ElMessage
}
from
'element-plus'
const
designSettingStore
=
useDesignSettingStore
()
const
currentRoute
=
useRoute
()
const
router
=
useRouter
()
const
menuValue
=
ref
(
currentRoute
.
meta
.
key
)
const
menuValue
=
ref
<
string
>
((
currentRoute
.
meta
.
key
as
string
)
||
''
)
watch
(
()
=>
currentRoute
.
fullPath
,
()
=>
{
menuValue
.
value
=
currentRoute
.
meta
.
key
menuValue
.
value
=
currentRoute
.
meta
.
key
as
string
},
)
...
...
@@ -24,8 +25,14 @@ function handleLogoClick() {
router
.
push
({
name
:
'Root'
})
}
function
handleMenuItemClick
(
routeName
:
any
)
{
router
.
push
({
name
:
routeName
})
function
handleMenuItemClick
(
menuInfo
:
MenuOption
)
{
if
(
menuInfo
.
routeName
)
{
router
.
push
({
name
:
menuInfo
.
routeName
})
}
else
if
(
menuInfo
.
path
)
{
router
.
push
(
menuInfo
.
path
)
}
else
{
ElMessage
.
error
(
'菜单项配置错误'
)
}
}
</
script
>
...
...
@@ -42,12 +49,12 @@ function handleMenuItemClick(routeName: any) {
class=
"!border-none"
>
<template
v-for=
"menuInfo in sidebarMenus"
>
<template
v-if=
"!menuInfo.children"
>
<template
v-if=
"!menuInfo.children
|| menuInfo.children.length === 0
"
>
<el-menu-item
:key=
"menuInfo.key"
:index=
"menuInfo.key"
class=
"relative hover:!bg-[unset]"
@
click=
"handleMenuItemClick(menuInfo
.routeName
)"
@
click=
"handleMenuItemClick(menuInfo)"
>
<span
class=
"absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition"
...
...
@@ -102,7 +109,7 @@ function handleMenuItemClick(routeName: any) {
<el-menu-item
:index=
"menuChildInfo.key"
class=
"group relative hover:!bg-[unset]"
@
click=
"handleMenuItemClick(menuChildInfo
.routeName
)"
@
click=
"handleMenuItemClick(menuChildInfo)"
>
<span
class=
"absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition"
...
...
src/layout/index.vue
View file @
44cb8fff
...
...
@@ -42,10 +42,12 @@ watchEffect(() => {
<NavBar
class=
"h-full"
/>
</el-header>
<el-main
class=
"main-content-wrapper"
>
<RouterView
v-slot=
"
{ Component }">
<el-main
class=
"main-content-wrapper
!overflow-hidden
"
>
<RouterView
v-slot=
"
{ Component
, route
}">
<Transition
appear
name=
"fade-slide"
mode=
"out-in"
>
<Component
:is=
"Component"
/>
<div
:key=
"route.fullPath"
class=
"h-full overflow-hidden"
>
<Component
:is=
"Component"
/>
</div>
</Transition>
</RouterView>
</el-main>
...
...
src/router/utils.ts
View file @
44cb8fff
...
...
@@ -3,12 +3,11 @@ import { nanoid } from 'nanoid/non-secure'
import
{
createWebHashHistory
,
createWebHistory
,
type
RouteRecordRaw
,
type
RouterHistory
}
from
'vue-router'
export
interface
MenuOption
{
// label: (() => VNode) | string
label
:
string
key
:
string
routeName
:
string
// icon: (() => VNode) | null
icon
:
string
path
:
string
children
?:
MenuOption
[]
}
...
...
@@ -37,16 +36,20 @@ function menuSort(routes: RouteRecordRaw[]) {
}
export
function
menuFilterSort
(
routes
:
RouteRecordRaw
[])
{
function
createRouteKey
(
routes
:
RouteRecordRaw
[])
{
function
createRouteKey
(
routes
:
RouteRecordRaw
[],
parentRoute
:
(
RouteRecordRaw
&
{
meta
:
{
key
:
string
}
})
|
null
=
null
,
)
{
routes
.
forEach
((
route
,
index
)
=>
{
if
(
route
.
meta
)
{
route
.
meta
.
key
=
nanoid
()
route
.
meta
.
key
=
route
.
meta
.
hideSideMenItem
&&
parentRoute
&&
parentRoute
.
meta
.
key
?
parentRoute
.
meta
.
key
:
nanoid
()
}
else
{
route
.
meta
=
{
key
:
nanoid
(),
title
:
''
,
rank
:
1001
+
index
}
route
.
meta
=
{
key
:
parentRoute
?.
meta
?.
key
||
nanoid
(),
title
:
''
,
rank
:
1001
+
index
}
}
if
(
route
.
children
)
{
createRouteKey
(
route
.
children
)
createRouteKey
(
route
.
children
,
route
as
RouteRecordRaw
&
{
meta
:
{
key
:
string
}
}
)
}
})
}
...
...
@@ -73,7 +76,7 @@ export function menuFilterSort(routes: RouteRecordRaw[]) {
routes
.
unshift
(...
rootRouteChildren
)
}
function
createMenuOptions
(
routes
:
RouteRecordRaw
[])
{
function
createMenuOptions
(
routes
:
RouteRecordRaw
[]
,
parentRoute
:
RouteRecordRaw
|
null
=
null
)
{
const
menuOptions
:
MenuOption
[]
=
[]
routes
.
forEach
((
route
)
=>
{
...
...
@@ -83,18 +86,15 @@ export function menuFilterSort(routes: RouteRecordRaw[]) {
}
const
menuOption
:
MenuOption
=
{
// label: route.meta?.title
// ? () => h(RouterLink, { to: { name: route.name || 'Root' } }, { default: () => route.meta?.title })
// : '-',
label
:
(
route
.
meta
?.
title
as
string
)
||
'-'
,
key
:
route
.
meta
?.
key
as
string
,
routeName
:
route
.
name
as
string
,
// icon: route.meta?.icon ? () => h('i', { class: `iconfont ${route?.meta?.icon as string}` }) : null,
icon
:
route
.
meta
?.
icon
||
''
,
path
:
(
route
.
path
=
route
.
path
.
startsWith
(
'/'
)
?
route
.
path
:
`
${
parentRoute
?.
path
}
/${route.path}`
)
,
}
if
(
route
.
children
)
{
menuOption
.
children
=
createMenuOptions
(
route
.
children
)
menuOption
.
children
=
createMenuOptions
(
route
.
children
,
route
)
}
menuOptions
.
push
(
menuOption
)
...
...
types/router.d.ts
View file @
44cb8fff
...
...
@@ -5,7 +5,8 @@ export {}
declare
module
'vue-router'
{
interface
RouteMeta
{
rank
:
number
title
:
string
title
?
:
string
icon
?:
string
hideSideMenItem
?:
boolean
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment