Skip to content

Commit 6805636

Browse files
committed
perf: 页面切换性能优化
1 parent c06ce94 commit 6805636

File tree

7 files changed

+22
-53
lines changed

7 files changed

+22
-53
lines changed

src/layout/components/sidebar/horizontal.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ watch(
5656
mode="horizontal"
5757
class="horizontal-header-menu"
5858
:default-active="route.path"
59-
@select="indexPath => menuSelect(indexPath, routers)"
6059
>
6160
<sidebar-item
6261
v-for="route in usePermissionStoreHook().wholeMenus"

src/layout/components/sidebar/vertical.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ onBeforeUnmount(() => {
9090
:collapse="isCollapse"
9191
:default-active="route.path"
9292
:collapse-transition="false"
93-
@select="indexPath => menuSelect(indexPath, routers)"
9493
>
9594
<sidebar-item
9695
v-for="routes in menuData"

src/layout/components/tag/index.vue

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { emitter } from "@/utils/mitt";
44
import { RouteConfigs } from "../../types";
55
import { useTags } from "../../hooks/useTag";
66
import { routerArrays } from "@/layout/types";
7-
import { isEqual, isAllEmpty } from "@pureadmin/utils";
87
import { handleAliveRoute, getTopMenu } from "@/router/utils";
98
import { useSettingStoreHook } from "@/store/modules/settings";
9+
import { useResizeObserver, useFullscreen } from "@vueuse/core";
10+
import { isEqual, isAllEmpty, debounce } from "@pureadmin/utils";
1011
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
1112
import { ref, watch, unref, toRaw, nextTick, onBeforeUnmount } from "vue";
12-
import { useResizeObserver, useDebounceFn, useFullscreen } from "@vueuse/core";
1313
1414
import ExitFullscreen from "@iconify-icons/ri/fullscreen-exit-fill";
1515
import Fullscreen from "@iconify-icons/ri/fullscreen-fill";
@@ -54,20 +54,22 @@ const topPath = getTopMenu()?.path;
5454
const { VITE_HIDE_HOME } = import.meta.env;
5555
const { isFullscreen, toggle } = useFullscreen();
5656
57-
const dynamicTagView = () => {
57+
const dynamicTagView = async () => {
58+
await nextTick();
5859
const index = multiTags.value.findIndex(item => {
59-
if (item.query) {
60+
if (!isAllEmpty(route.query)) {
6061
return isEqual(route.query, item.query);
61-
} else if (item.params) {
62+
} else if (!isAllEmpty(route.params)) {
6263
return isEqual(route.params, item.params);
6364
} else {
64-
return item.path === route.path;
65+
return route.path === item.path;
6566
}
6667
});
6768
moveToView(index);
6869
};
6970
7071
const moveToView = async (index: number): Promise<void> => {
72+
await nextTick();
7173
const tabNavPadding = 10;
7274
if (!instance.refs["dynamic" + index]) return;
7375
const tabItemEl = instance.refs["dynamic" + index][0];
@@ -78,9 +80,6 @@ const moveToView = async (index: number): Promise<void> => {
7880
? scrollbarDom.value?.offsetWidth
7981
: 0;
8082
81-
// 获取视图更新后dom
82-
await nextTick();
83-
8483
// 已有标签页总长度(包含溢出部分)
8584
const tabDomWidth = tabDom.value ? tabDom.value?.offsetWidth : 0;
8685
@@ -135,31 +134,29 @@ const handleScroll = (offset: number): void => {
135134
}
136135
};
137136
138-
function dynamicRouteTag(value: string, parentPath: string): void {
137+
function dynamicRouteTag(value: string): void {
139138
const hasValue = multiTags.value.some(item => {
140139
return item.path === value;
141140
});
142141
143-
function concatPath(arr: object[], value: string, parentPath: string) {
142+
function concatPath(arr: object[], value: string) {
144143
if (!hasValue) {
145144
arr.forEach((arrItem: any) => {
146-
const pathConcat = parentPath + arrItem.path;
147-
if (arrItem.path === value || pathConcat === value) {
145+
if (arrItem.path === value || arrItem.path === value) {
148146
useMultiTagsStoreHook().handleTags("push", {
149147
path: value,
150-
parentPath: `/${parentPath.split("/")[1]}`,
151148
meta: arrItem.meta,
152149
name: arrItem.name
153150
});
154151
} else {
155152
if (arrItem.children && arrItem.children.length > 0) {
156-
concatPath(arrItem.children, value, parentPath);
153+
concatPath(arrItem.children, value);
157154
}
158155
}
159156
});
160157
}
161158
}
162-
concatPath(router.options.routes as any, value, parentPath);
159+
concatPath(router.options.routes as any, value);
163160
}
164161
165162
/** 刷新路由 */
@@ -465,7 +462,7 @@ function tagOnClick(item) {
465462
// showMenuModel(item?.path, item?.query);
466463
}
467464
468-
watch([route], () => {
465+
watch(route, () => {
469466
activeIndex.value = -1;
470467
dynamicTagView();
471468
});
@@ -493,18 +490,16 @@ onMounted(() => {
493490
});
494491
495492
// 接收侧边栏切换传递过来的参数
496-
emitter.on("changLayoutRoute", ({ indexPath, parentPath }) => {
497-
dynamicRouteTag(indexPath, parentPath);
493+
emitter.on("changLayoutRoute", ({ indexPath }) => {
494+
dynamicRouteTag(indexPath);
498495
setTimeout(() => {
499496
showMenuModel(indexPath);
500497
});
501498
});
502499
503500
useResizeObserver(
504501
scrollbarDom,
505-
useDebounceFn(() => {
506-
dynamicTagView();
507-
}, 200)
502+
debounce(() => dynamicTagView())
508503
);
509504
});
510505

src/layout/hooks/useNav.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,34 +114,14 @@ export function useNav() {
114114
}
115115
}
116116

117-
function menuSelect(indexPath: string, routers): void {
117+
function menuSelect(indexPath: string): void {
118118
if (wholeMenus.value.length === 0) return;
119119
if (isRemaining(indexPath)) return;
120-
let parentPath = "";
121-
const parentPathIndex = indexPath.lastIndexOf("/");
122-
if (parentPathIndex > 0) {
123-
parentPath = indexPath.slice(0, parentPathIndex);
124-
}
120+
125121
/** 找到当前路由的信息 */
126-
function findCurrentRoute(indexPath: string, routes) {
127-
if (!routes) return console.error(errorInfo);
128-
return routes.map(item => {
129-
if (item.path === indexPath) {
130-
if (item.redirect) {
131-
findCurrentRoute(item.redirect, item.children);
132-
} else {
133-
/** 切换左侧菜单 通知标签页 */
134-
emitter.emit("changLayoutRoute", {
135-
indexPath,
136-
parentPath
137-
});
138-
}
139-
} else {
140-
if (item.children) findCurrentRoute(indexPath, item.children);
141-
}
142-
});
143-
}
144-
findCurrentRoute(indexPath, routers);
122+
emitter.emit("changLayoutRoute", {
123+
indexPath
124+
});
145125
}
146126

147127
/** 判断路径是否参与菜单 */

src/layout/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const routerArrays: Array<RouteConfigs> =
66
? [
77
{
88
path: "/welcome",
9-
parentPath: "/",
109
meta: {
1110
title: "menus.hshome",
1211
icon: "homeFilled"
@@ -25,7 +24,6 @@ export type routeMetaType = {
2524

2625
export type RouteConfigs = {
2726
path?: string;
28-
parentPath?: string;
2927
query?: object;
3028
params?: object;
3129
meta?: routeMetaType;

src/store/modules/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export type appType = {
2424

2525
export type multiType = {
2626
path: string;
27-
parentPath: string;
2827
name: string;
2928
meta: any;
3029
query?: object;

src/utils/mitt.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type Events = {
1414
logoChange: boolean;
1515
changLayoutRoute: {
1616
indexPath: string;
17-
parentPath: string;
1817
};
1918
};
2019

0 commit comments

Comments
 (0)