优化导航守卫路由动态注册,将404页面修改为固定路由

This commit is contained in:
hooray
2022-06-24 00:11:37 +08:00
parent 50a979cfab
commit a8d061ccff
3 changed files with 17 additions and 21 deletions
+7 -9
View File
@@ -9,10 +9,8 @@ import '@/assets/styles/nprogress.scss'
import { useNProgress } from '@vueuse/integrations/useNProgress'
const { isLoading } = useNProgress()
import generatedRoutes from 'virtual:generated-pages'
// 路由相关数据
import { constantRoutes, asyncRoutes, notFoundRoute } from './routes'
import { constantRoutes, asyncRoutes } from './routes'
const router = createRouter({
history: createWebHashHistory(),
@@ -87,18 +85,18 @@ router.beforeEach(async(to, from, next) => {
removeRoutes.push(router.addRoute(route))
}
})
if (settingsStore.app.routeBaseOn === 'filesystem') {
const otherRoutes = generatedRoutes.filter(item => item.meta?.constant !== true && item.meta?.layout === false)
otherRoutes.length && removeRoutes.push(router.addRoute(...otherRoutes))
} else {
if (settingsStore.app.routeBaseOn !== 'filesystem') {
routeStore.flatSystemRoutes.forEach(route => {
removeRoutes.push(router.addRoute(route))
})
removeRoutes.push(router.addRoute(notFoundRoute))
}
// 记录的 accessRoutes 路由数据,在登出时会使用到,不使用 router.removeRoute 是考虑配置的路由可能不一定有设置 name ,则通过调用 router.addRoute() 返回的回调进行删除
routeStore.setCurrentRemoveRoutes(removeRoutes)
next({ ...to, replace: true })
next({
path: to.fullPath,
query: to.query,
replace: true
})
}
} else {
if (to.name != 'login') {
+9 -12
View File
@@ -14,6 +14,14 @@ let constantRoutes = [
meta: {
title: '登录'
}
},
{
path: '/:all(.*)*',
name: 'notFound',
component: () => import('@/views/[...all].vue'),
meta: {
title: '找不到页面'
}
}
]
@@ -122,19 +130,8 @@ if (useSettingsStore(pinia).app.routeBaseOn === 'filesystem') {
}))
}
// 404路由
const notFoundRoute = {
path: '/:all(.*)*',
name: 'notFound',
component: () => import('@/views/[...all].vue'),
meta: {
title: '找不到页面'
}
}
export {
constantRoutes,
systemRoutes,
asyncRoutes,
notFoundRoute
asyncRoutes
}
+1
View File
@@ -3,6 +3,7 @@
name: 'notFound',
meta: {
title: "找不到页面",
constant: true,
layout: false
}
}