Blame view

src/logics/mitt/routeChange.ts 874 Bytes
vben authored
1
2
3
4
/**
 * 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
 */
5
import mitt from '/@/utils/mitt';
vben authored
6
import type { RouteLocationNormalized } from 'vue-router';
7
import { getRawRoute } from '/@/utils';
vben authored
8
9
const emitter = mitt();
vben authored
10
11
12
13
14

const key = Symbol();

let lastChangeTab: RouteLocationNormalized;
15
export function setRouteChange(lastChangeRoute: RouteLocationNormalized) {
16
  const r = getRawRoute(lastChangeRoute);
17
  emitter.emit(key, r);
18
  lastChangeTab = r;
vben authored
19
20
}
21
export function listenerRouteChange(
vben authored
22
23
24
  callback: (route: RouteLocationNormalized) => void,
  immediate = true
) {
25
  emitter.on(key, callback);
vben authored
26
  immediate && lastChangeTab && callback(lastChangeTab);
vben authored
27
}
28
29

export function removeTabChangeListener() {
30
  emitter.clear();
31
}