Skip to content

Commit

Permalink
fix(router): 修复权限问题 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
huccct committed Jan 10, 2024
1 parent 7e4d66b commit bc5f880
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { createRouter, createWebHashHistory } from 'vue-router'
import { constantRoute } from './routes'

let router = createRouter({
const router = createRouter({
history: createWebHashHistory(),
routes: constantRoute,
// 滚动行为
Expand Down
14 changes: 9 additions & 5 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { constantRoute, asyncRoute, anyRoute } from '@/router/routes'
// @ts-ignore
import cloneDeep from 'lodash/cloneDeep'

let dynamicRoutes = []

Check failure on line 23 in src/store/modules/user.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Variable 'dynamicRoutes' implicitly has type 'any[]' in some locations where its type cannot be determined.

function filterAsyncRoute(asyncRoute: any, routes: any) {
return asyncRoute.filter((item: any) => {
if (routes.includes(item.name)) {
Expand Down Expand Up @@ -64,16 +66,15 @@ let useUserStore = defineStore('User', {
if (res.code === 200) {
this.username = res.data.name as string
this.avatar = res.data.avatar as string

let userAsyncRoute = filterAsyncRoute(
cloneDeep(asyncRoute),
res.data.routes,
)
this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute]
;[...userAsyncRoute, anyRoute].forEach((route: any) => {
console.log(route)

router.addRoute(route)
console.log('router', router)
dynamicRoutes = [...userAsyncRoute, anyRoute] // 记录动态添加的路由
dynamicRoutes.forEach((route) => {
router.addRoute(route) // 动态添加路由
})
return 'ok'
} else {
Expand All @@ -87,6 +88,9 @@ let useUserStore = defineStore('User', {
this.username = ''
this.avatar = ''
REMOVE_TOKEN()
dynamicRoutes.forEach((route) => {

Check failure on line 91 in src/store/modules/user.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Variable 'dynamicRoutes' implicitly has an 'any[]' type.
router.removeRoute(route.name)
})
} else {
return Promise.reject(new Error(res.message))
}
Expand Down

0 comments on commit bc5f880

Please sign in to comment.