vue-vben-admin/src/hooks/web/usePage.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type { RouteLocationRaw } from 'vue-router';
import { PageEnum } from '/@/enums/pageEnum';
import { isString } from '/@/utils/is';
import { unref } from 'vue';
2020-12-03 21:49:32 +08:00
import router from '/@/router';
2020-09-28 20:19:10 +08:00
export type RouteLocationRawEx = Omit<RouteLocationRaw, 'path'> & { path: PageEnum };
function handleError(e: Error) {
console.error(e);
}
// page switch
export function useGo() {
2020-12-03 21:49:32 +08:00
const { push, replace } = router;
2020-10-18 21:55:21 +08:00
function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) {
2020-11-23 23:24:13 +08:00
if (!opt) return;
2020-09-28 20:19:10 +08:00
if (isString(opt)) {
isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
} else {
const o = opt as RouteLocationRaw;
isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);
}
}
return go;
}
/**
* @description: redo current page
*/
export const useRedo = () => {
2020-12-03 21:49:32 +08:00
const { push, currentRoute } = router;
const { query, params } = currentRoute.value;
2020-12-15 00:13:23 +08:00
function redo(): Promise<boolean> {
return new Promise((resolve) => {
push({
path: '/redirect' + unref(currentRoute).fullPath,
query,
params,
}).then(() => resolve(true));
2020-09-28 20:19:10 +08:00
});
}
return redo;
};