feat(router): add NProgress

This commit is contained in:
xiangshu233 2024-03-06 22:45:45 +08:00
parent 302bc7d955
commit 977aae6d85
3 changed files with 22 additions and 1 deletions

View File

@ -47,6 +47,7 @@
"echarts": "^5.4.3",
"lodash-es": "^4.17.21",
"mockjs": "^1.1.0",
"nprogress": "^0.2.0",
"pinia": "^2.1.7",
"pinia-plugin-persist": "^1.0.0",
"qs": "^6.11.2",
@ -62,6 +63,7 @@
"@types/fs-extra": "^11.0.4",
"@types/mockjs": "^1.0.10",
"@types/node": "^20.10.5",
"@types/nprogress": "^0.2.3",
"@types/qs": "^6.9.11",
"@unocss/eslint-plugin": "^0.58.4",
"@unocss/preset-icons": "^0.58.5",

14
pnpm-lock.yaml generated
View File

@ -29,6 +29,9 @@ dependencies:
mockjs:
specifier: ^1.1.0
version: 1.1.0
nprogress:
specifier: ^0.2.0
version: 0.2.0
pinia:
specifier: ^2.1.7
version: 2.1.7(typescript@5.3.3)(vue@3.3.13)
@ -70,6 +73,9 @@ devDependencies:
'@types/node':
specifier: ^20.10.5
version: 20.10.5
'@types/nprogress':
specifier: ^0.2.3
version: 0.2.3
'@types/qs':
specifier: ^6.9.11
version: 6.9.11
@ -1645,6 +1651,10 @@ packages:
resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==}
dev: true
/@types/nprogress@0.2.3:
resolution: {integrity: sha512-k7kRA033QNtC+gLc4VPlfnue58CM1iQLgn1IMAU8VPHGOj7oIHPp9UlhedEnD/Gl8evoCjwkZjlBORtZ3JByUA==}
dev: true
/@types/qs@6.9.11:
resolution: {integrity: sha512-oGk0gmhnEJK4Yyk+oI7EfXsLayXatCWPHary1MtcmbAifkobT9cM9yutG/hZKIseOU0MqbIwQ/u2nn/Gb+ltuQ==}
dev: true
@ -5553,6 +5563,10 @@ packages:
path-key: 4.0.0
dev: true
/nprogress@0.2.0:
resolution: {integrity: sha512-I19aIingLgR1fmhftnbWWO3dXc0hSxqHQHQb3H8m+K3TnEn/iSeTZZOyvKXWqQESMwuUVnatlCnZdLBZZt2VSA==}
dev: false
/nth-check@2.1.1:
resolution: {integrity: sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==}
dependencies:

View File

@ -1,10 +1,14 @@
import type { Router } from 'vue-router'
import { isNavigationFailure } from 'vue-router'
import NProgress from 'nprogress'
import { useRouteStoreWidthOut } from '@/store/modules/route'
import { useUserStoreWidthOut } from '@/store/modules/user'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { storage } from '@/utils/Storage'
import { PageEnum } from '@/enums/pageEnum'
import 'nprogress/nprogress.css'
NProgress.configure({ parent: '#app' })
const LOGIN_PATH = PageEnum.BASE_LOGIN
@ -14,7 +18,7 @@ export function createRouterGuards(router: Router) {
router.beforeEach(async (to, from, next) => {
// to: 即将要进入的目标
// from: 当前导航正要离开的路由
NProgress.start()
const userStore = useUserStoreWidthOut()
if (from.path === LOGIN_PATH && to.name === PageEnum.ERROR_PAGE_NAME) {
@ -83,6 +87,7 @@ export function createRouterGuards(router: Router) {
}
}
routeStore.setKeepAliveComponents(keepAliveComponents)
NProgress.done()
})
router.onError((error) => {