vben
authored
5 years ago
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
*/
Vben
authored
4 years ago
5
import mitt from '/@/utils/mitt';
vben
authored
5 years ago
6
import type { RouteLocationNormalized } from 'vue-router';
Vben
authored
4 years ago
7
import { getRawRoute } from '/@/utils';
vben
authored
5 years ago
8
Vben
authored
4 years ago
9
const emitter = mitt();
vben
authored
5 years ago
10
11
12
13
14
const key = Symbol();
let lastChangeTab: RouteLocationNormalized;
Vben
authored
4 years ago
15
export function setRouteChange(lastChangeRoute: RouteLocationNormalized) {
Vben
authored
4 years ago
16
const r = getRawRoute(lastChangeRoute);
Vben
authored
4 years ago
17
emitter.emit(key, r);
vben
authored
5 years ago
18
lastChangeTab = r;
vben
authored
5 years ago
19
20
}
Vben
authored
4 years ago
21
export function listenerRouteChange(
vben
authored
5 years ago
22
23
24
callback: (route: RouteLocationNormalized) => void,
immediate = true
) {
Vben
authored
4 years ago
25
emitter.on(key, callback);
vben
authored
4 years ago
26
immediate && lastChangeTab && callback(lastChangeTab);
vben
authored
5 years ago
27
}
vben
authored
5 years ago
28
29
export function removeTabChangeListener() {
Vben
authored
4 years ago
30
emitter.clear();
vben
authored
5 years ago
31
}