mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-07 17:07:19 +08:00
perf: 页面切换性能优化 (#600)
* perf: 页面切换性能优化 * fix: 修复刷新页面时`router.beforeEach`调用两次的问题
This commit is contained in:
parent
aec2a35424
commit
5d86b714a4
@ -1,7 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { ref, nextTick } from "vue";
|
||||||
import Search from "../search/index.vue";
|
import Search from "../search/index.vue";
|
||||||
import Notice from "../notice/index.vue";
|
import Notice from "../notice/index.vue";
|
||||||
import { ref, watch, nextTick } from "vue";
|
|
||||||
import SidebarItem from "./sidebarItem.vue";
|
import SidebarItem from "./sidebarItem.vue";
|
||||||
import { useNav } from "@/layout/hooks/useNav";
|
import { useNav } from "@/layout/hooks/useNav";
|
||||||
import { useTranslationLang } from "../../hooks/useTranslationLang";
|
import { useTranslationLang } from "../../hooks/useTranslationLang";
|
||||||
@ -17,11 +17,9 @@ const { t, route, locale, translationCh, translationEn } =
|
|||||||
useTranslationLang(menuRef);
|
useTranslationLang(menuRef);
|
||||||
const {
|
const {
|
||||||
title,
|
title,
|
||||||
routers,
|
|
||||||
logout,
|
logout,
|
||||||
backTopMenu,
|
backTopMenu,
|
||||||
onPanel,
|
onPanel,
|
||||||
menuSelect,
|
|
||||||
username,
|
username,
|
||||||
userAvatar,
|
userAvatar,
|
||||||
avatarsStyle,
|
avatarsStyle,
|
||||||
@ -32,13 +30,6 @@ const {
|
|||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
menuRef.value?.handleResize();
|
menuRef.value?.handleResize();
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
|
||||||
() => route.path,
|
|
||||||
() => {
|
|
||||||
menuSelect(route.path, routers);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -56,7 +47,6 @@ watch(
|
|||||||
mode="horizontal"
|
mode="horizontal"
|
||||||
class="horizontal-header-menu"
|
class="horizontal-header-menu"
|
||||||
:default-active="route.path"
|
:default-active="route.path"
|
||||||
@select="indexPath => menuSelect(indexPath, routers)"
|
|
||||||
>
|
>
|
||||||
<sidebar-item
|
<sidebar-item
|
||||||
v-for="route in usePermissionStoreHook().wholeMenus"
|
v-for="route in usePermissionStoreHook().wholeMenus"
|
||||||
|
@ -21,10 +21,8 @@ const { t, route, locale, translationCh, translationEn } =
|
|||||||
useTranslationLang(menuRef);
|
useTranslationLang(menuRef);
|
||||||
const {
|
const {
|
||||||
device,
|
device,
|
||||||
routers,
|
|
||||||
logout,
|
logout,
|
||||||
onPanel,
|
onPanel,
|
||||||
menuSelect,
|
|
||||||
resolvePath,
|
resolvePath,
|
||||||
username,
|
username,
|
||||||
userAvatar,
|
userAvatar,
|
||||||
@ -72,7 +70,6 @@ watch(
|
|||||||
mode="horizontal"
|
mode="horizontal"
|
||||||
class="horizontal-header-menu"
|
class="horizontal-header-menu"
|
||||||
:default-active="defaultActive"
|
:default-active="defaultActive"
|
||||||
@select="indexPath => menuSelect(indexPath, routers)"
|
|
||||||
>
|
>
|
||||||
<el-menu-item
|
<el-menu-item
|
||||||
v-for="route in usePermissionStoreHook().wholeMenus"
|
v-for="route in usePermissionStoreHook().wholeMenus"
|
||||||
|
@ -18,8 +18,7 @@ const showLogo = ref(
|
|||||||
)?.showLogo ?? true
|
)?.showLogo ?? true
|
||||||
);
|
);
|
||||||
|
|
||||||
const { routers, device, pureApp, isCollapse, menuSelect, toggleSideBar } =
|
const { device, pureApp, isCollapse, menuSelect, toggleSideBar } = useNav();
|
||||||
useNav();
|
|
||||||
|
|
||||||
const subMenuData = ref([]);
|
const subMenuData = ref([]);
|
||||||
|
|
||||||
@ -56,7 +55,7 @@ watch(
|
|||||||
() => {
|
() => {
|
||||||
if (route.path.includes("/redirect")) return;
|
if (route.path.includes("/redirect")) return;
|
||||||
getSubMenuData(route.path);
|
getSubMenuData(route.path);
|
||||||
menuSelect(route.path, routers);
|
menuSelect(route.path);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -90,7 +89,6 @@ onBeforeUnmount(() => {
|
|||||||
:collapse="isCollapse"
|
:collapse="isCollapse"
|
||||||
:default-active="route.path"
|
:default-active="route.path"
|
||||||
:collapse-transition="false"
|
:collapse-transition="false"
|
||||||
@select="indexPath => menuSelect(indexPath, routers)"
|
|
||||||
>
|
>
|
||||||
<sidebar-item
|
<sidebar-item
|
||||||
v-for="routes in menuData"
|
v-for="routes in menuData"
|
||||||
|
@ -4,12 +4,12 @@ import { emitter } from "@/utils/mitt";
|
|||||||
import { RouteConfigs } from "../../types";
|
import { RouteConfigs } from "../../types";
|
||||||
import { useTags } from "../../hooks/useTag";
|
import { useTags } from "../../hooks/useTag";
|
||||||
import { routerArrays } from "@/layout/types";
|
import { routerArrays } from "@/layout/types";
|
||||||
import { isEqual, isAllEmpty } from "@pureadmin/utils";
|
|
||||||
import { handleAliveRoute, getTopMenu } from "@/router/utils";
|
import { handleAliveRoute, getTopMenu } from "@/router/utils";
|
||||||
import { useSettingStoreHook } from "@/store/modules/settings";
|
import { useSettingStoreHook } from "@/store/modules/settings";
|
||||||
|
import { useResizeObserver, useFullscreen } from "@vueuse/core";
|
||||||
|
import { isEqual, isAllEmpty, debounce } from "@pureadmin/utils";
|
||||||
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
|
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
|
||||||
import { ref, watch, unref, toRaw, nextTick, onBeforeUnmount } from "vue";
|
import { ref, watch, unref, toRaw, nextTick, onBeforeUnmount } from "vue";
|
||||||
import { useResizeObserver, useDebounceFn, useFullscreen } from "@vueuse/core";
|
|
||||||
|
|
||||||
import ExitFullscreen from "@iconify-icons/ri/fullscreen-exit-fill";
|
import ExitFullscreen from "@iconify-icons/ri/fullscreen-exit-fill";
|
||||||
import Fullscreen from "@iconify-icons/ri/fullscreen-fill";
|
import Fullscreen from "@iconify-icons/ri/fullscreen-fill";
|
||||||
@ -54,20 +54,22 @@ const topPath = getTopMenu()?.path;
|
|||||||
const { VITE_HIDE_HOME } = import.meta.env;
|
const { VITE_HIDE_HOME } = import.meta.env;
|
||||||
const { isFullscreen, toggle } = useFullscreen();
|
const { isFullscreen, toggle } = useFullscreen();
|
||||||
|
|
||||||
const dynamicTagView = () => {
|
const dynamicTagView = async () => {
|
||||||
|
await nextTick();
|
||||||
const index = multiTags.value.findIndex(item => {
|
const index = multiTags.value.findIndex(item => {
|
||||||
if (item.query) {
|
if (!isAllEmpty(route.query)) {
|
||||||
return isEqual(route.query, item.query);
|
return isEqual(route.query, item.query);
|
||||||
} else if (item.params) {
|
} else if (!isAllEmpty(route.params)) {
|
||||||
return isEqual(route.params, item.params);
|
return isEqual(route.params, item.params);
|
||||||
} else {
|
} else {
|
||||||
return item.path === route.path;
|
return route.path === item.path;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
moveToView(index);
|
moveToView(index);
|
||||||
};
|
};
|
||||||
|
|
||||||
const moveToView = async (index: number): Promise<void> => {
|
const moveToView = async (index: number): Promise<void> => {
|
||||||
|
await nextTick();
|
||||||
const tabNavPadding = 10;
|
const tabNavPadding = 10;
|
||||||
if (!instance.refs["dynamic" + index]) return;
|
if (!instance.refs["dynamic" + index]) return;
|
||||||
const tabItemEl = instance.refs["dynamic" + index][0];
|
const tabItemEl = instance.refs["dynamic" + index][0];
|
||||||
@ -78,9 +80,6 @@ const moveToView = async (index: number): Promise<void> => {
|
|||||||
? scrollbarDom.value?.offsetWidth
|
? scrollbarDom.value?.offsetWidth
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
// 获取视图更新后dom
|
|
||||||
await nextTick();
|
|
||||||
|
|
||||||
// 已有标签页总长度(包含溢出部分)
|
// 已有标签页总长度(包含溢出部分)
|
||||||
const tabDomWidth = tabDom.value ? tabDom.value?.offsetWidth : 0;
|
const tabDomWidth = tabDom.value ? tabDom.value?.offsetWidth : 0;
|
||||||
|
|
||||||
@ -135,31 +134,29 @@ const handleScroll = (offset: number): void => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function dynamicRouteTag(value: string, parentPath: string): void {
|
function dynamicRouteTag(value: string): void {
|
||||||
const hasValue = multiTags.value.some(item => {
|
const hasValue = multiTags.value.some(item => {
|
||||||
return item.path === value;
|
return item.path === value;
|
||||||
});
|
});
|
||||||
|
|
||||||
function concatPath(arr: object[], value: string, parentPath: string) {
|
function concatPath(arr: object[], value: string) {
|
||||||
if (!hasValue) {
|
if (!hasValue) {
|
||||||
arr.forEach((arrItem: any) => {
|
arr.forEach((arrItem: any) => {
|
||||||
const pathConcat = parentPath + arrItem.path;
|
if (arrItem.path === value || arrItem.path === value) {
|
||||||
if (arrItem.path === value || pathConcat === value) {
|
|
||||||
useMultiTagsStoreHook().handleTags("push", {
|
useMultiTagsStoreHook().handleTags("push", {
|
||||||
path: value,
|
path: value,
|
||||||
parentPath: `/${parentPath.split("/")[1]}`,
|
|
||||||
meta: arrItem.meta,
|
meta: arrItem.meta,
|
||||||
name: arrItem.name
|
name: arrItem.name
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (arrItem.children && arrItem.children.length > 0) {
|
if (arrItem.children && arrItem.children.length > 0) {
|
||||||
concatPath(arrItem.children, value, parentPath);
|
concatPath(arrItem.children, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
concatPath(router.options.routes as any, value, parentPath);
|
concatPath(router.options.routes as any, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 刷新路由 */
|
/** 刷新路由 */
|
||||||
@ -465,7 +462,7 @@ function tagOnClick(item) {
|
|||||||
// showMenuModel(item?.path, item?.query);
|
// showMenuModel(item?.path, item?.query);
|
||||||
}
|
}
|
||||||
|
|
||||||
watch([route], () => {
|
watch(route, () => {
|
||||||
activeIndex.value = -1;
|
activeIndex.value = -1;
|
||||||
dynamicTagView();
|
dynamicTagView();
|
||||||
});
|
});
|
||||||
@ -493,8 +490,8 @@ onMounted(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 接收侧边栏切换传递过来的参数
|
// 接收侧边栏切换传递过来的参数
|
||||||
emitter.on("changLayoutRoute", ({ indexPath, parentPath }) => {
|
emitter.on("changLayoutRoute", indexPath => {
|
||||||
dynamicRouteTag(indexPath, parentPath);
|
dynamicRouteTag(indexPath);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
showMenuModel(indexPath);
|
showMenuModel(indexPath);
|
||||||
});
|
});
|
||||||
@ -502,9 +499,7 @@ onMounted(() => {
|
|||||||
|
|
||||||
useResizeObserver(
|
useResizeObserver(
|
||||||
scrollbarDom,
|
scrollbarDom,
|
||||||
useDebounceFn(() => {
|
debounce(() => dynamicTagView())
|
||||||
dynamicTagView();
|
|
||||||
}, 200)
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -114,38 +114,13 @@ export function useNav() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function menuSelect(indexPath: string, routers): void {
|
function menuSelect(indexPath: string) {
|
||||||
if (wholeMenus.value.length === 0) return;
|
if (wholeMenus.value.length === 0 || isRemaining(indexPath)) return;
|
||||||
if (isRemaining(indexPath)) return;
|
emitter.emit("changLayoutRoute", indexPath);
|
||||||
let parentPath = "";
|
|
||||||
const parentPathIndex = indexPath.lastIndexOf("/");
|
|
||||||
if (parentPathIndex > 0) {
|
|
||||||
parentPath = indexPath.slice(0, parentPathIndex);
|
|
||||||
}
|
|
||||||
/** 找到当前路由的信息 */
|
|
||||||
function findCurrentRoute(indexPath: string, routes) {
|
|
||||||
if (!routes) return console.error(errorInfo);
|
|
||||||
return routes.map(item => {
|
|
||||||
if (item.path === indexPath) {
|
|
||||||
if (item.redirect) {
|
|
||||||
findCurrentRoute(item.redirect, item.children);
|
|
||||||
} else {
|
|
||||||
/** 切换左侧菜单 通知标签页 */
|
|
||||||
emitter.emit("changLayoutRoute", {
|
|
||||||
indexPath,
|
|
||||||
parentPath
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (item.children) findCurrentRoute(indexPath, item.children);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
findCurrentRoute(indexPath, routers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 判断路径是否参与菜单 */
|
/** 判断路径是否参与菜单 */
|
||||||
function isRemaining(path: string): boolean {
|
function isRemaining(path: string) {
|
||||||
return remainingPaths.includes(path);
|
return remainingPaths.includes(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ export const routerArrays: Array<RouteConfigs> =
|
|||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
path: "/welcome",
|
path: "/welcome",
|
||||||
parentPath: "/",
|
|
||||||
meta: {
|
meta: {
|
||||||
title: "menus.hshome",
|
title: "menus.hshome",
|
||||||
icon: "homeFilled"
|
icon: "homeFilled"
|
||||||
@ -25,7 +24,6 @@ export type routeMetaType = {
|
|||||||
|
|
||||||
export type RouteConfigs = {
|
export type RouteConfigs = {
|
||||||
path?: string;
|
path?: string;
|
||||||
parentPath?: string;
|
|
||||||
query?: object;
|
query?: object;
|
||||||
params?: object;
|
params?: object;
|
||||||
meta?: routeMetaType;
|
meta?: routeMetaType;
|
||||||
|
@ -176,7 +176,8 @@ router.beforeEach((to: toRouteType, _from, next) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
router.push(to.fullPath);
|
// 确保动态路由完全加入路由列表并且不影响静态路由(注意:动态路由刷新时router.beforeEach可能会触发两次,第一次触发动态路由还未完全添加,第二次动态路由才完全添加到路由列表,如果需要在router.beforeEach做一些判断可以在to.name存在的条件下去判断,这样就只会触发一次)
|
||||||
|
if (isAllEmpty(to.name)) router.push(to.fullPath);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
toCorrectRoute();
|
toCorrectRoute();
|
||||||
|
@ -24,7 +24,6 @@ export type appType = {
|
|||||||
|
|
||||||
export type multiType = {
|
export type multiType = {
|
||||||
path: string;
|
path: string;
|
||||||
parentPath: string;
|
|
||||||
name: string;
|
name: string;
|
||||||
meta: any;
|
meta: any;
|
||||||
query?: object;
|
query?: object;
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
import type { Emitter } from "mitt";
|
import type { Emitter } from "mitt";
|
||||||
import mitt from "mitt";
|
import mitt from "mitt";
|
||||||
|
|
||||||
|
/** 全局公共事件需要在此处添加类型 */
|
||||||
type Events = {
|
type Events = {
|
||||||
resize: {
|
|
||||||
detail: {
|
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
openPanel: string;
|
openPanel: string;
|
||||||
tagViewsChange: string;
|
tagViewsChange: string;
|
||||||
tagViewsShowModel: string;
|
tagViewsShowModel: string;
|
||||||
logoChange: boolean;
|
logoChange: boolean;
|
||||||
changLayoutRoute: {
|
changLayoutRoute: string;
|
||||||
indexPath: string;
|
|
||||||
parentPath: string;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const emitter: Emitter<Events> = mitt<Events>();
|
export const emitter: Emitter<Events> = mitt<Events>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user