You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
736 B
TypeScript

import type { RouteLocationNormalized } from 'vue-router/auto'
import { getRawRoute, mitt } from '~/utils'
export const emitter = mitt()
const key = Symbol('router-mitt')
let lastChangeTab: RouteLocationNormalized
export function setRouteChange(lastChangeRoute: RouteLocationNormalized) {
const r = getRawRoute(lastChangeRoute)
emitter.emit(key, r)
lastChangeTab = r
}
export function listenerRouteChange(
callback: (route: RouteLocationNormalized) => void,
immediate = true,
) {
// @ts-expect-error 不知道为什么报错,但是可以用
emitter.on<RouteLocationNormalized>(key, callback)
immediate && lastChangeTab && callback(lastChangeTab)
}
export function removeTabChangeListener() {
emitter.clear()
}