import { createRouter, createWebHashHistory, createWebHistory, } from 'vue-router'; import { resetStaticRoutes } from '@vben/utils'; import { setupChunkLoadRecovery } from '#/utils/chunk-load-recovery'; import { createRouterGuard } from './guard'; import { routes } from './routes'; /** * @zh_CN 创建vue-router实例 */ const router = createRouter({ // 根据环境变量配置选择合适的路由历史模式 history: import.meta.env.VITE_ROUTER_HISTORY === 'hash' ? createWebHashHistory(import.meta.env.VITE_BASE) : createWebHistory(import.meta.env.VITE_BASE), // 应该添加到路由的初始路由列表。 routes, // 自定义滚动行为以提升用户体验 scrollBehavior: (to, _from, savedPosition) => { // 如果有保存的位置,则恢复到该位置 if (savedPosition) { return savedPosition; } // 如果路由带有哈希,则平滑滚动到该哈希对应的元素;否则滚动到页面顶部 return to.hash ? { behavior: 'smooth', el: to.hash } : { left: 0, top: 0 }; }, // 是否应该禁止尾部斜杠。 // strict: true, }); const resetRoutes = () => resetStaticRoutes(router, routes); // 创建路由守卫 createRouterGuard(router); // 菜单懒加载 chunk 在发版后失效时自动硬刷新(与 main 里的全局监听互补) setupChunkLoadRecovery(router); export { resetRoutes, router };