Files
JeecgBoot/jeecgboot-vue3/src/router/router.ts
2025-06-25 16:04:02 +08:00

43 lines
1.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 路由实例存储文件,请勿轻易添加其他代码,防止出现 HMR 或其他问题
*/
import type {Router, RouterHistory} from 'vue-router';
import {createRouter as createVueRouter, createWebHistory, createWebHashHistory, RouterOptions} from 'vue-router';
export let router: Router = null as unknown as Router;
export function setRouter(r: Router) {
router = r
}
let webHistory: Nullable<RouterHistory> = null;
/**
* 创建路由
* @param options 参数
* @param useHashHistory 是否使用 hash 路由true使用false不使用hash路由
*/
export function createRouter(options: Partial<RouterOptions>, useHashHistory = false) {
const createFn = useHashHistory ? createWebHashHistory : createWebHistory;
webHistory = createFn(import.meta.env.VITE_PUBLIC_PATH);
// app router
let router = createVueRouter({
history: webHistory,
routes: [],
...options,
});
setRouter(router)
return router
}
// 销毁路由
export function destroyRouter() {
setRouter(null as unknown as Router);
if (webHistory) {
webHistory.destroy();
}
webHistory = null
}