pageLoadingGuard.ts 829 B

1234567891011121314151617181920212223242526272829303132
  1. import type { Router } from 'vue-router';
  2. import { appStore } from '/@/store/modules/app';
  3. import { userStore } from '/@/store/modules/user';
  4. import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
  5. import { unref } from 'vue';
  6. const { getOpenPageLoading } = useTransitionSetting();
  7. export function createPageLoadingGuard(router: Router) {
  8. router.beforeEach(async (to) => {
  9. if (!userStore.getTokenState) {
  10. return true;
  11. }
  12. if (to.meta.loaded) {
  13. return true;
  14. }
  15. if (unref(getOpenPageLoading)) {
  16. appStore.setPageLoadingAction(true);
  17. return true;
  18. }
  19. return true;
  20. });
  21. router.afterEach(async () => {
  22. if (unref(getOpenPageLoading)) {
  23. setTimeout(() => {
  24. appStore.commitPageLoadingState(false);
  25. }, 300);
  26. }
  27. return true;
  28. });
  29. }