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