diff --git a/src/hooks/web/usePage.ts b/src/hooks/web/usePage.ts index 738f3e3d..67e2c289 100644 --- a/src/hooks/web/usePage.ts +++ b/src/hooks/web/usePage.ts @@ -1,35 +1,28 @@ import type { RouteLocationRaw, Router } from 'vue-router'; import { PageEnum } from '/@/enums/pageEnum'; -import { isString } from '/@/utils/is'; import { unref } from 'vue'; import { useRouter } from 'vue-router'; import { REDIRECT_NAME } from '/@/router/constant'; -export type RouteLocationRawEx = RouteLocationRaw & { path: PageEnum }; +export type PathAsPageEnum = T extends { path: string } ? T & { path: PageEnum } : T; +export type RouteLocationRawEx = PathAsPageEnum; function handleError(e: Error) { console.error(e); } -// page switch +/** + * page switch + */ export function useGo(_router?: Router) { - let router; - if (!_router) { - router = useRouter(); - } - const { push, replace } = _router || router; - function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) { + const { push, replace } = _router || useRouter(); + function go(opt: RouteLocationRawEx = PageEnum.BASE_HOME, isReplace = false) { if (!opt) { return; } - 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); - } + isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError); } return go; }