【修复】修复刷新浏览器路由警告问题

pull/90/head
小诺 2023-03-12 21:14:31 +08:00 committed by 俞宝山
parent cd3b44e6ba
commit dece5567ba
1 changed files with 31 additions and 45 deletions

View File

@ -8,11 +8,8 @@
* 5.不可二次分发开源参与同类竞品如有想法可联系团队xiaonuobase@qq.com商议合作 * 5.不可二次分发开源参与同类竞品如有想法可联系团队xiaonuobase@qq.com商议合作
* 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip * 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip
*/ */
/* eslint-disable eqeqeq */ import { createRouter, createWebHistory } from 'vue-router'
/* eslint-disable camelcase */
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { notification } from 'ant-design-vue' import { notification } from 'ant-design-vue'
import config from '@/config'
import NProgress from 'nprogress' import NProgress from 'nprogress'
import 'nprogress/nprogress.css' import 'nprogress/nprogress.css'
import systemRouter from './systemRouter' import systemRouter from './systemRouter'
@ -28,19 +25,18 @@ const sysBaseConfig = tool.data.get('SNOWY_SYS_BASE_CONFIG') || store.state.glob
// 进度条配置 // 进度条配置
NProgress.configure({ showSpinner: false, speed: 500 }) NProgress.configure({ showSpinner: false, speed: 500 })
// 系统路由
const routes = [...systemRouter, ...whiteListRouters]
// 系统特殊路由 // 系统特殊路由
const routes_404 = { const routes_404 = [
path: '/:pathMatch(.*)*', {
hidden: true, path: '/:pathMatch(.*)*',
component: () => import('@/layout/other/404.vue') hidden: true,
} component: () => import('@/layout/other/404.vue')
let routes_404_r = () => {} }
]
// 系统路由
const routes = [...systemRouter, ...whiteListRouters, ...routes_404]
const router = createRouter({ const router = createRouter({
// 此方式不带 # 号 // createWebHashHistory()带#号
history: createWebHistory(), history: createWebHistory(),
routes routes
}) })
@ -49,7 +45,7 @@ const router = createRouter({
document.title = sysBaseConfig.SNOWY_SYS_NAME document.title = sysBaseConfig.SNOWY_SYS_NAME
// 判断是否已加载过动态/静态路由 // 判断是否已加载过动态/静态路由
let isGetRouter = false const isGetRouter = ref(false)
// 白名单校验 // 白名单校验
const exportWhiteListFromRouter = (router) => { const exportWhiteListFromRouter = (router) => {
@ -59,32 +55,6 @@ const exportWhiteListFromRouter = (router) => {
} }
const whiteList = exportWhiteListFromRouter(whiteListRouters) const whiteList = exportWhiteListFromRouter(whiteListRouters)
// 加载动态/静态路由
const handleGetRouter = (to) => {
if (!isGetRouter) {
let apiMenu = tool.data.get('MENU') || []
if (apiMenu.length === 0) {
// 创建默认模块,显示默认菜单
apiMenu[0] = cloneDeep(userRoutes.module[0])
const userMenu = userRoutes.menu
const childrenApiMenu = apiMenu[0].children
apiMenu[0].children = [...userMenu, ...childrenApiMenu]
}
let menuRouter = filterAsyncRouter(apiMenu)
menuRouter = flatAsyncRoutes(menuRouter)
menuRouter.forEach((item) => {
router.addRoute('layout', item)
})
store.commit('search/init', menuRouter)
routes_404_r = router.addRoute(routes_404)
if (to && to.matched.length === 0) {
router.push(to.fullPath)
}
isGetRouter = true
}
}
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
NProgress.start() NProgress.start()
// 动态标题 // 动态标题
@ -110,9 +80,7 @@ router.beforeEach(async (to, from, next) => {
} }
// 删除路由(替换当前layout路由) // 删除路由(替换当前layout路由)
router.addRoute(routes[0]) router.addRoute(routes[0])
// 删除路由(404) isGetRouter.value = false
routes_404_r()
isGetRouter = false
next() next()
return false return false
} }
@ -127,7 +95,25 @@ router.beforeEach(async (to, from, next) => {
to.matched = [to.matched[to.matched.length - 1]] to.matched = [to.matched[to.matched.length - 1]]
} }
// 加载动态/静态路由 // 加载动态/静态路由
handleGetRouter(to) if (!isGetRouter.value) {
const apiMenu = tool.data.get('MENU') || []
if (apiMenu.length === 0) {
// 创建默认模块,显示默认菜单
apiMenu[0] = cloneDeep(userRoutes.module[0])
const userMenu = userRoutes.menu
const childrenApiMenu = apiMenu[0].children
apiMenu[0].children = [...userMenu, ...childrenApiMenu]
}
let menuRouter = filterAsyncRouter(apiMenu)
menuRouter = flatAsyncRoutes(menuRouter)
menuRouter.forEach((item) => {
router.addRoute('layout', item)
})
store.commit('search/init', menuRouter)
isGetRouter.value = true
next({ ...to, replace: true })
return false
}
beforeEach(to, from) beforeEach(to, from)
next() next()
}) })