mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-09-16 11:34:55 +08:00
perf: 优化nprogress
进度条,页面重进或接口请求时不再显示进度条,提升用户体验 (#1225)
This commit is contained in:
parent
c821f32ae9
commit
915e01c15c
@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { $t } from "@/plugins/i18n";
|
||||
import { emitter } from "@/utils/mitt";
|
||||
import NProgress from "@/utils/progress";
|
||||
import { RouteConfigs } from "../../types";
|
||||
import { useTags } from "../../hooks/useTag";
|
||||
import { routerArrays } from "@/layout/types";
|
||||
@ -206,12 +207,14 @@ function dynamicRouteTag(value: string): void {
|
||||
|
||||
/** 刷新路由 */
|
||||
function onFresh() {
|
||||
NProgress.start();
|
||||
const { fullPath, query } = unref(route);
|
||||
router.replace({
|
||||
path: "/redirect" + fullPath,
|
||||
query
|
||||
});
|
||||
handleAliveRoute(route as ToRouteType, "refresh");
|
||||
NProgress.done();
|
||||
}
|
||||
|
||||
function deleteDynamicTag(obj: any, current: any, tag?: string) {
|
||||
|
@ -94,6 +94,14 @@ export const router: Router = createRouter({
|
||||
}
|
||||
});
|
||||
|
||||
/** 记录已经加载的页面路径 */
|
||||
const loadedPaths = new Set<string>();
|
||||
|
||||
/** 重置已加载页面记录 */
|
||||
export function resetLoadedPaths() {
|
||||
loadedPaths.clear();
|
||||
}
|
||||
|
||||
/** 重置路由 */
|
||||
export function resetRouter() {
|
||||
router.clearRoutes();
|
||||
@ -104,6 +112,7 @@ export function resetRouter() {
|
||||
formatFlatteningRoutes(buildHierarchyTree(ascending(routes.flat(Infinity))))
|
||||
);
|
||||
usePermissionStoreHook().clearAllCachePage();
|
||||
resetLoadedPaths();
|
||||
}
|
||||
|
||||
/** 路由白名单 */
|
||||
@ -112,6 +121,12 @@ const whiteList = ["/login"];
|
||||
const { VITE_HIDE_HOME } = import.meta.env;
|
||||
|
||||
router.beforeEach((to: ToRouteType, _from, next) => {
|
||||
to.meta.loaded = loadedPaths.has(to.path);
|
||||
|
||||
if (!to.meta.loaded) {
|
||||
NProgress.start();
|
||||
}
|
||||
|
||||
if (to.meta?.keepAlive) {
|
||||
handleAliveRoute(to, "add");
|
||||
// 页面整体刷新和点击标签页刷新
|
||||
@ -120,7 +135,6 @@ router.beforeEach((to: ToRouteType, _from, next) => {
|
||||
}
|
||||
}
|
||||
const userInfo = storageLocal().getItem<DataInfo<number>>(userKey);
|
||||
NProgress.start();
|
||||
const externalLink = isUrl(to?.name as string);
|
||||
if (!externalLink) {
|
||||
to.matched.some(item => {
|
||||
@ -206,7 +220,8 @@ router.beforeEach((to: ToRouteType, _from, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.afterEach(() => {
|
||||
router.afterEach(to => {
|
||||
loadedPaths.add(to.path);
|
||||
NProgress.done();
|
||||
});
|
||||
|
||||
|
@ -10,7 +10,6 @@ import type {
|
||||
PureHttpRequestConfig
|
||||
} from "./types.d";
|
||||
import { stringify } from "qs";
|
||||
import NProgress from "../progress";
|
||||
import { getToken, formatToken } from "@/utils/auth";
|
||||
import { useUserStoreHook } from "@/store/modules/user";
|
||||
|
||||
@ -61,8 +60,6 @@ class PureHttp {
|
||||
private httpInterceptorsRequest(): void {
|
||||
PureHttp.axiosInstance.interceptors.request.use(
|
||||
async (config: PureHttpRequestConfig): Promise<any> => {
|
||||
// 开启进度条动画
|
||||
NProgress.start();
|
||||
// 优先判断post/get等方法是否传入回调,否则执行初始化设置等回调
|
||||
if (typeof config.beforeRequestCallback === "function") {
|
||||
config.beforeRequestCallback(config);
|
||||
@ -121,8 +118,6 @@ class PureHttp {
|
||||
instance.interceptors.response.use(
|
||||
(response: PureHttpResponse) => {
|
||||
const $config = response.config;
|
||||
// 关闭进度条动画
|
||||
NProgress.done();
|
||||
// 优先判断post/get等方法是否传入回调,否则执行初始化设置等回调
|
||||
if (typeof $config.beforeResponseCallback === "function") {
|
||||
$config.beforeResponseCallback(response);
|
||||
@ -137,8 +132,6 @@ class PureHttp {
|
||||
(error: PureHttpError) => {
|
||||
const $error = error;
|
||||
$error.isCancelRequest = Axios.isCancel($error);
|
||||
// 关闭进度条动画
|
||||
NProgress.done();
|
||||
// 所有的响应异常 区分来源为取消请求/非取消请求
|
||||
return Promise.reject($error);
|
||||
}
|
||||
|
2
types/router.d.ts
vendored
2
types/router.d.ts
vendored
@ -56,6 +56,8 @@ declare global {
|
||||
* 而通过设置`activePath`指定激活菜单即可获得高亮,`activePath`为指定激活菜单的`path`)
|
||||
*/
|
||||
activePath?: string;
|
||||
/** 当前页面是否已经加载过 */
|
||||
loaded?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user