import { createRouter, createWebHistory } from 'vue-router'; import { constantRoutes } from './routes'; const router = createRouter({ history: createWebHistory(import.meta.env.VITE_BASE_URL || '/'), routes: constantRoutes, scrollBehavior: () => ({ left: 0, top: 0 }), }); /** * 路由白名单:不需要登录即可访问的路径 */ const WHITE_LIST = ['/login', '/404']; /** * 静态路由名称集合(这些路由不会被 resetRouter 移除) */ const STATIC_ROUTE_NAMES = new Set(['Login', 'NotFound']); /** * 重置路由:清除动态添加的路由(包括 BasicLayout 和其子路由), * 恢复到只包含静态路由的状态。用于退出登录时清理。 */ export function resetRouter() { const currentRoutes = router.getRoutes(); currentRoutes.forEach((route) => { const routeName = route.name as string; // 保留静态路由(Login, NotFound),移除所有动态路由(含 BasicLayout 及其子路由) if (routeName && !STATIC_ROUTE_NAMES.has(routeName)) { router.removeRoute(routeName); } }); } export default router; export { WHITE_LIST };