index.ts 978 B

123456789101112131415161718192021222324252627282930313233
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import { useRouteStore } from '@/stores'
  3. import { constantRoutes, systemRoutes } from '@/router/route'
  4. import { setupRouterGuard } from '@/router/guard'
  5. const router = createRouter({
  6. history: createWebHashHistory(import.meta.env.BASE_URL),
  7. routes: [...constantRoutes, ...systemRoutes],
  8. scrollBehavior: () => ({ left: 0, top: 0 }),
  9. })
  10. setupRouterGuard(router)
  11. /**
  12. * @description 重置路由
  13. * @description 注意:所有动态路由路由必须带有 name 属性,否则可能会不能完全重置干净
  14. */
  15. export function resetRouter() {
  16. try {
  17. const routeStore = useRouteStore()
  18. routeStore.asyncRoutes.forEach((route) => {
  19. const { name } = route
  20. if (name) {
  21. router.hasRoute(name) && router.removeRoute(name)
  22. }
  23. })
  24. } catch (error) {
  25. // 强制刷新浏览器也行,只是交互体验不是很好
  26. window.location.reload()
  27. }
  28. }
  29. export default router