refactor: 优化路由守卫代码(同步 GiDemo 更新)

This commit is contained in:
2024-12-08 19:56:35 +08:00
parent abacb267aa
commit 7c509fa737
3 changed files with 68 additions and 59 deletions

View File

@@ -9,8 +9,6 @@ import ArcoVueIcon from '@arco-design/web-vue/es/icon'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
import '@/router/guard'
// 使用动画库 // 使用动画库
import 'animate.css/animate.min.css' import 'animate.css/animate.min.css'
@@ -25,6 +23,8 @@ import 'virtual:svg-icons-register'
// 自定义指令 // 自定义指令
import directives from './directives' import directives from './directives'
// 状态管理
import pinia from '@/stores' import pinia from '@/stores'
// 对特定组件进行默认配置 // 对特定组件进行默认配置

View File

@@ -1,6 +1,6 @@
import { Button, Message, Notification, Space } from '@arco-design/web-vue' import { Button, Message, Notification, Space } from '@arco-design/web-vue'
import NProgress from 'nprogress' import NProgress from 'nprogress'
import router from '@/router' import type { Router } from 'vue-router'
import { useRouteStore, useUserStore } from '@/stores' import { useRouteStore, useUserStore } from '@/stores'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import { isHttp } from '@/utils/validate' import { isHttp } from '@/utils/validate'
@@ -36,7 +36,10 @@ const handleNotification = () => {
closable: true, closable: true,
position: 'bottomRight', position: 'bottomRight',
footer: () => { footer: () => {
return h(Space, {}, () => [h(Button, { type: 'primary', onClick: () => onUpdateSystem(id) }, '更新'), h(Button, { type: 'secondary', onClick: () => onCloseUpdateSystem(id) }, '关闭')]) return h(Space, {}, () => [h(Button, {
type: 'primary',
onClick: () => onUpdateSystem(id),
}, '更新'), h(Button, { type: 'secondary', onClick: () => onCloseUpdateSystem(id) }, '关闭')])
}, },
}) })
} }
@@ -75,6 +78,8 @@ export const resetHasRouteFlag = () => {
hasRouteFlag = false hasRouteFlag = false
} }
/** 初始化路由守卫 */
export const setupRouterGuard = (router: Router) => {
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
NProgress.start() NProgress.start()
const userStore = useUserStore() const userStore = useUserStore()
@@ -136,3 +141,4 @@ router.onError(() => {
router.afterEach(() => { router.afterEach(() => {
NProgress.done() NProgress.done()
}) })
}

View File

@@ -1,6 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router' import { createRouter, createWebHistory } from 'vue-router'
import { useRouteStore } from '@/stores' import { useRouteStore } from '@/stores'
import { constantRoutes, systemRoutes } from '@/router/route' import { constantRoutes, systemRoutes } from '@/router/route'
import { setupRouterGuard } from '@/router/guard'
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
@@ -8,6 +9,8 @@ const router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }), scrollBehavior: () => ({ left: 0, top: 0 }),
}) })
setupRouterGuard(router)
/** /**
* @description 重置路由 * @description 重置路由
* @description 注意:所有动态路由路由必须带有 name 属性,否则可能会不能完全重置干净 * @description 注意:所有动态路由路由必须带有 name 属性,否则可能会不能完全重置干净