Commit 02c469b17a02ca870b4204b230fc3b4272296a56

Authored by Tanimodori
Committed by GitHub
1 parent deff31bc

types: 再次修复RouteLocationRawEx类型错误 (vbenjs#1968) (#1975)

Showing 1 changed file with 8 additions and 15 deletions
src/hooks/web/usePage.ts
1 import type { RouteLocationRaw, Router } from 'vue-router'; 1 import type { RouteLocationRaw, Router } from 'vue-router';
2 2
3 import { PageEnum } from '/@/enums/pageEnum'; 3 import { PageEnum } from '/@/enums/pageEnum';
4 -import { isString } from '/@/utils/is';  
5 import { unref } from 'vue'; 4 import { unref } from 'vue';
6 5
7 import { useRouter } from 'vue-router'; 6 import { useRouter } from 'vue-router';
8 import { REDIRECT_NAME } from '/@/router/constant'; 7 import { REDIRECT_NAME } from '/@/router/constant';
9 8
10 -export type RouteLocationRawEx = RouteLocationRaw & { path: PageEnum }; 9 +export type PathAsPageEnum<T> = T extends { path: string } ? T & { path: PageEnum } : T;
  10 +export type RouteLocationRawEx = PathAsPageEnum<RouteLocationRaw>;
11 11
12 function handleError(e: Error) { 12 function handleError(e: Error) {
13 console.error(e); 13 console.error(e);
14 } 14 }
15 15
16 -// page switch 16 +/**
  17 + * page switch
  18 + */
17 export function useGo(_router?: Router) { 19 export function useGo(_router?: Router) {
18 - let router;  
19 - if (!_router) {  
20 - router = useRouter();  
21 - }  
22 - const { push, replace } = _router || router;  
23 - function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) { 20 + const { push, replace } = _router || useRouter();
  21 + function go(opt: RouteLocationRawEx = PageEnum.BASE_HOME, isReplace = false) {
24 if (!opt) { 22 if (!opt) {
25 return; 23 return;
26 } 24 }
27 - if (isString(opt)) {  
28 - isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);  
29 - } else {  
30 - const o = opt as RouteLocationRaw;  
31 - isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);  
32 - } 25 + isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
33 } 26 }
34 return go; 27 return go;
35 } 28 }