Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
credit
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
niuyutian
credit
Commits
2a916455
Commit
2a916455
authored
Sep 07, 2023
by
jiangweijia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
路由相关改动,添加页面跳转方法
parent
4cd63863
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
4 deletions
+51
-4
Menu.tsx
src/layouts/default/components/Menu.tsx
+1
-1
app.ts
src/store/modules/app.ts
+50
-3
No files found.
src/layouts/default/components/Menu.tsx
View file @
2a916455
...
...
@@ -17,7 +17,7 @@ export const TheMenu = defineComponent({
setup
(
props
)
{
const
menuStore
=
useMenuStore
()
const
{
menu
,
selectedMenu
,
openedMenu
}
=
storeToRefs
(
menuStore
)
function
handleOpenChange
(
openKeys
:
any
)
{
menuStore
.
setOpenedMenu
([...
openKeys
])
}
...
...
src/store/modules/app.ts
View file @
2a916455
...
...
@@ -3,7 +3,7 @@ import { setupLayouts } from 'virtual:generated-layouts'
import
generatedRoutes
from
'virtual:generated-pages'
import
{
postGetUserInfo
,
postQueryMenu
}
from
'@/apis'
import
{
useConfigStore
,
useMenuStore
,
useUserStore
}
from
'@/store/modules'
import
{
message
}
from
'ant-design-vue'
export
const
useAppStore
=
defineStore
(
'app'
,
()
=>
{
const
route
=
location
.
hash
.
slice
(
1
)
const
userId
=
localStorage
.
getItem
(
'userId'
)
...
...
@@ -12,6 +12,7 @@ export const useAppStore = defineStore('app', () => {
const
menuStore
=
useMenuStore
()
const
configStore
=
useConfigStore
()
const
userStore
=
useUserStore
()
const
traansformRouter
:
any
=
ref
([])
if
(
!
userId
)
{
location
.
hash
=
'#/login'
...
...
@@ -45,6 +46,22 @@ export const useAppStore = defineStore('app', () => {
}
})
}
function
transformToRoute
(
routerList
:
any
[],
item
:
Menu
[])
{
let
flatB
=
flatten
(
item
)
let
pathToTitle
:
any
=
{}
for
(
let
i
=
0
;
i
<
flatB
.
length
;
i
++
)
{
pathToTitle
[
flatB
[
i
].
path
]
=
flatB
[
i
].
title
}
let
result
=
routerList
.
map
((
item
)
=>
{
if
(
item
.
path
in
pathToTitle
)
{
return
{
...
item
,
meta
:
{
title
:
pathToTitle
[
item
.
path
]
}
}
}
else
{
return
item
}
})
return
result
}
// 递归查找菜单
function
findMenu
(
menu
:
MenuRouteConfig
[],
openedMenu
:
MenuRouteConfig
[]
=
[])
{
...
...
@@ -77,11 +94,40 @@ export const useAppStore = defineStore('app', () => {
menuStore
.
setMenu
(
menu
)
}
function
setRoutes
()
{
function
setRoutes
(
data
:
any
[]
)
{
const
routes
=
generatedRoutes
.
filter
((
r
)
=>
authRoutes
.
includes
(
r
.
path
))
traansformRouter
.
value
=
transformToRoute
(
routes
,
data
)
router
.
addRoutes
(
setupLayouts
(
routes
))
}
function
getRoutes
(
name
:
string
)
{
return
traansformRouter
.
value
.
find
((
item
:
{
name
:
string
})
=>
item
.
name
==
name
)
}
// 打开新标签页
function
openPage
(
pageName
:
string
)
{
let
targMenu
=
getRoutes
(
pageName
)
if
(
targMenu
?.
path
)
{
menuStore
.
addVisitedMenu
(
targMenu
)
menuStore
.
setSelectedMenu
(
targMenu
.
name
)
router
.
push
(
targMenu
.
path
)
}
else
{
message
.
warning
(
'未找到菜单'
)
}
}
// 平铺数组
function
flatten
(
arr
:
any
[])
{
let
result
:
any
[]
=
[]
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
if
(
arr
[
i
].
children
)
{
result
=
result
.
concat
(
flatten
(
arr
[
i
].
children
))
}
else
{
result
.
push
(
arr
[
i
])
}
}
return
result
}
function
setState
()
{
let
openedMenu
:
MenuRouteConfig
[]
=
[]
let
currentMenu
=
findMenu
(
menuStore
.
menu
,
openedMenu
)
...
...
@@ -101,7 +147,7 @@ export const useAppStore = defineStore('app', () => {
postQueryMenu
().
then
((
r
)
=>
{
if
(
r
.
data
)
{
setMenu
(
r
.
data
)
setRoutes
()
setRoutes
(
r
.
data
)
setState
()
loading
.
value
=
false
}
...
...
@@ -116,6 +162,7 @@ export const useAppStore = defineStore('app', () => {
return
{
loading
,
openPage
,
logout
}
})
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