From 977aae6d859c600d798ecc1705e81fa707e3f5b2 Mon Sep 17 00:00:00 2001 From: xiangshu233 Date: Wed, 6 Mar 2024 22:45:45 +0800 Subject: [PATCH] feat(router): :sparkles: add NProgress --- package.json | 2 ++ pnpm-lock.yaml | 14 ++++++++++++++ src/router/router-guards.ts | 7 ++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d665b84..1cc7bef 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2820296..c939e4d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: diff --git a/src/router/router-guards.ts b/src/router/router-guards.ts index c172690..2622476 100644 --- a/src/router/router-guards.ts +++ b/src/router/router-guards.ts @@ -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) => {