Skip to content

Commit a15c09c

Browse files
author
奇淼(piexlmax
authored
1 parent dbdd2d1 commit a15c09c

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

web/src/permission.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import getPageTitle from '@/utils/page'
44
import router from '@/router'
55
import Nprogress from 'nprogress'
66

7-
let asyncRouterFlag = 0
8-
97
const whiteList = ['Login', 'Init']
108

119
const getRouter = async(userStore) => {
@@ -38,6 +36,7 @@ async function handleKeepAlive(to) {
3836
}
3937

4038
router.beforeEach(async(to, from) => {
39+
const routerStore = useRouterStore()
4140
Nprogress.start()
4241
const userStore = useUserStore()
4342
to.meta.matched = [...to.matched]
@@ -47,8 +46,7 @@ router.beforeEach(async(to, from) => {
4746
document.title = getPageTitle(to.meta.title, to)
4847
if (whiteList.indexOf(to.name) > -1) {
4948
if (token) {
50-
if (!asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
51-
asyncRouterFlag++
49+
if (!routerStore.asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
5250
await getRouter(userStore)
5351
}
5452
// token 可以解析但是却是不存在的用户 id 或角色 id 会导致无限调用
@@ -71,8 +69,7 @@ router.beforeEach(async(to, from) => {
7169
// 不在白名单中并且已经登录的时候
7270
if (token) {
7371
// 添加flag防止多次获取动态路由和栈溢出
74-
if (!asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
75-
asyncRouterFlag++
72+
if (!routerStore.asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
7673
await getRouter(userStore)
7774
if (userStore.token) {
7875
return { ...to, replace: true }

web/src/pinia/modules/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const KeepAliveFilter = (routes) => {
4747

4848
export const useRouterStore = defineStore('router', () => {
4949
const keepAliveRouters = ref([])
50+
const asyncRouterFlag = ref(0)
5051
const setKeepAliveRouters = (history) => {
5152
const keepArrTemp = []
5253
history.forEach(item => {
@@ -63,6 +64,7 @@ export const useRouterStore = defineStore('router', () => {
6364
const routeMap = ({})
6465
// 从后台获取动态路由
6566
const SetAsyncRouter = async() => {
67+
asyncRouterFlag.value++
6668
const baseRouter = [{
6769
path: '/layout',
6870
name: 'layout',
@@ -114,6 +116,7 @@ export const useRouterStore = defineStore('router', () => {
114116
asyncRouters,
115117
routerList,
116118
keepAliveRouters,
119+
asyncRouterFlag,
117120
SetAsyncRouter,
118121
routeMap
119122
}

0 commit comments

Comments
 (0)