index.ts 948 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {
  2. createRouter,
  3. createWebHashHistory,
  4. createWebHistory,
  5. } from 'vue-router';
  6. import { resetStaticRoutes } from '@vben/utils';
  7. import { createRouterGuard } from './guard';
  8. import { routes } from './routes';
  9. console.log(routes);
  10. /**
  11. * @zh_CN 创建vue-router实例
  12. */
  13. const router = createRouter({
  14. history:
  15. import.meta.env.VITE_ROUTER_HISTORY === 'hash'
  16. ? createWebHashHistory(import.meta.env.VITE_BASE)
  17. : createWebHistory(import.meta.env.VITE_BASE),
  18. // 应该添加到路由的初始路由列表。
  19. routes,
  20. scrollBehavior: (to, _from, savedPosition) => {
  21. if (savedPosition) {
  22. return savedPosition;
  23. }
  24. return to.hash ? { behavior: 'smooth', el: to.hash } : { left: 0, top: 0 };
  25. },
  26. // 是否应该禁止尾部斜杠。
  27. // strict: true,
  28. });
  29. const resetRoutes = () => resetStaticRoutes(router, routes);
  30. // 创建路由守卫
  31. createRouterGuard(router);
  32. export { resetRoutes, router };