index.ts 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import type { Router } from 'vue-router';
  2. import { preference } from '@vben/preference';
  3. import { startProgress, stopProgress } from '@vben/utils';
  4. import { configAccessGuard } from './access';
  5. /**
  6. * 通用守卫配置
  7. * @param router
  8. */
  9. function configCommonGuard(router: Router) {
  10. const loadedPaths = new Set<string>();
  11. router.beforeEach(async (to) => {
  12. if (preference.pageProgress) {
  13. startProgress();
  14. }
  15. to.meta.loaded = loadedPaths.has(to.path);
  16. return true;
  17. });
  18. router.afterEach((to) => {
  19. // 记录页面是否加载,如果已经加载,后续的页面切换动画等效果不在重复执行
  20. loadedPaths.add(to.path);
  21. if (preference.pageProgress) {
  22. stopProgress();
  23. }
  24. });
  25. }
  26. /**
  27. * 项目守卫配置
  28. * @param router
  29. */
  30. function createRouteGuard(router: Router) {
  31. /** 通用 */
  32. configCommonGuard(router);
  33. /** 权限访问 */
  34. configAccessGuard(router);
  35. }
  36. export { createRouteGuard };