This commit is contained in:
xuziqiang
2024-05-15 17:29:42 +08:00
commit d0155dbe3c
7296 changed files with 1832517 additions and 0 deletions

53
lib/util/routeChange.ts Normal file
View File

@@ -0,0 +1,53 @@
/**
* Used to monitor routing changes to change the status of menus and tabs. There is no need to monitor the route, because the route status change is affected by the page rendering time, which will be slow
*
* @format
*/
import mitt from './mitt';
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
const emitter = mitt();
const key = Symbol();
const reloadKey = Symbol();
let lastChangeTab: RouteLocationNormalized;
export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
if (!route) return route;
const { matched, ...opt } = route;
return {
...opt,
matched: (matched
? matched.map((item) => ({
meta: item.meta,
name: item.name,
path: item.path,
}))
: undefined) as RouteRecordNormalized[],
};
}
export function setReloadChange() {
emitter.emit(reloadKey);
}
export function listenerReloadChange(callback: () => void, immediate = true) {
emitter.on(reloadKey, callback);
immediate && callback();
}
export function setRouteChange(lastChangeRoute: RouteLocationNormalized) {
const r = getRawRoute(lastChangeRoute);
emitter.emit(key, r);
lastChangeTab = r;
}
export function listenerRouteChange(
callback: (route: RouteLocationNormalized) => void,
immediate = true,
) {
emitter.on(key, callback);
immediate && lastChangeTab && callback(lastChangeTab);
}
export function removeTabChangeListener() {
emitter.clear();
}