usePage.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { appStore } from '/@/store/modules/app';
  2. import type { RouteLocationRaw } from 'vue-router';
  3. import { useRouter } from 'vue-router';
  4. import { PageEnum } from '/@/enums/pageEnum';
  5. import { isString } from '/@/utils/is';
  6. import { unref } from 'vue';
  7. export type RouteLocationRawEx = Omit<RouteLocationRaw, 'path'> & { path: PageEnum };
  8. function handleError(e: Error) {
  9. console.error(e);
  10. // 101是为了 大于 打开时候设置的100延时防止闪动
  11. setTimeout(() => {
  12. appStore.commitPageLoadingState(false);
  13. }, 101);
  14. }
  15. // page switch
  16. export function useGo() {
  17. const { push, replace } = useRouter();
  18. function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) {
  19. if (isString(opt)) {
  20. isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
  21. } else {
  22. const o = opt as RouteLocationRaw;
  23. isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);
  24. }
  25. }
  26. return go;
  27. }
  28. /**
  29. * @description: redo current page
  30. */
  31. export const useRedo = () => {
  32. const { push, currentRoute } = useRouter();
  33. function redo() {
  34. push({
  35. path: '/redirect' + unref(currentRoute).fullPath,
  36. });
  37. }
  38. return redo;
  39. };