Skip to content

Commit f3865a6

Browse files
committed
native jump relative path is available
1 parent 18c90c7 commit f3865a6

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/helpers/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,24 @@ export function assertParentChild(
425425
}
426426
return false
427427
}
428+
429+
export function resolveAbsolutePath(
430+
path:string,
431+
router:Router
432+
):string|never {
433+
const reg = /^\/?([^\?]+)(\?.+)?/;
434+
if (!reg.test(path)) {
435+
throw new Error(`${path} 路径错误,请提供完整的路径(10001)。`);
436+
}
437+
const paramsArray = path.match(reg);
438+
if (paramsArray == null) {
439+
throw new Error(`${path} 路径错误,请提供完整的路径(10002)。`);
440+
}
441+
const relative = paramsArray[1].replace(/\//g, `\\/`).replace(/\.\./g, `[^\\/]+`);
442+
const relativeReg = new RegExp(`^\\/${relative}$`);
443+
const route = router.options.routes.filter(it => relativeReg.test(it.path));
444+
if (route.length !== 1) {
445+
throw new Error(`${path} 路径错误,尝试转成绝对路径失败,请手动转成绝对路径(10003)。`);
446+
}
447+
return route[0].path + paramsArray[2];
448+
}

src/public/rewrite.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
routesForMapRoute,
1515
getRoutePath,
1616
getDataType,
17-
notDeepClearNull
17+
notDeepClearNull,
18+
resolveAbsolutePath
1819
} from '../helpers/utils'
1920

2021
import {
@@ -98,11 +99,9 @@ function callRouterMethod(
9899
const routerMethodName = rewriteMethodToggle[(funName as reNavMethodRule)]
99100
let path = (option as uniNavApiRule).url;
100101
if (!path.startsWith('/')) {
101-
warn(
102-
`uni-app 原生方法被重写时,只能使用绝对路径进行跳转。${JSON.stringify(option)}`,
103-
router,
104-
true
105-
);
102+
const absolutePath = resolveAbsolutePath(path, router);
103+
path = absolutePath;
104+
(option as uniNavApiRule).url = absolutePath;
106105
}
107106
if (funName === 'switchTab') {
108107
const route = routesForMapRoute(router, path, ['pathMap', 'finallyPathList'])

0 commit comments

Comments
 (0)