From c82b30d34864a6f88c6bfd5c7193e8404eb6a2c9 Mon Sep 17 00:00:00 2001 From: "shujian.cao" Date: Tue, 18 Apr 2023 19:04:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(runtime-web):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=BD=AC=20Web=20=E9=A1=B5=E9=9D=A2=E7=9B=B8=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E8=B7=B3=E8=BD=AC=E6=94=AF=E6=8C=81=20(#20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/runtime-web/src/router/api.ts | 5 +++-- packages/runtime-web/src/router/url.ts | 31 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/runtime-web/src/router/api.ts b/packages/runtime-web/src/router/api.ts index fc7adb2e..5342f7b4 100644 --- a/packages/runtime-web/src/router/api.ts +++ b/packages/runtime-web/src/router/api.ts @@ -3,7 +3,7 @@ import { history } from './history' import { getCurrentPages } from './pageStack' import { batchUnloadPage } from './router' import { IPage, IRouterApiParams } from './types' -import { getCustomUrl } from './url' +import { getAbsolutePath, getCustomUrl } from './url' interface INavigateTask { eventName: string @@ -14,7 +14,8 @@ const IDLE_TIME = 20 const NAVIGATE_TASKS: INavigateTask[] = [] function getTo(url: string) { - const urlArr = getCustomUrl(url).split('?') + const absolutePath = getAbsolutePath(url) + const urlArr = getCustomUrl(absolutePath).split('?') return { pathname: urlArr[0], search: urlArr[1] ? `?${urlArr[1]}` : '' diff --git a/packages/runtime-web/src/router/url.ts b/packages/runtime-web/src/router/url.ts index a7dced13..295777a2 100644 --- a/packages/runtime-web/src/router/url.ts +++ b/packages/runtime-web/src/router/url.ts @@ -169,3 +169,34 @@ export const getRelaunchOptions = () => { referrerInfo } } + +/** + * 根据页面相对路径,计算出绝对路径 '../swiper/index' => 'pages/swiper/index' + * @param {String} relativePath 相对路径 + * @returns 绝对路径 + */ +export const getAbsolutePath = (relativePath: string) => { + const rootPath = getCurrentPages().pop()?.route + + if (rootPath && relativePath[0] === '.') { + const rootParts = rootPath.split('/') + const relativeParts = relativePath.split('/') + const absoluteParts = [...relativeParts] + + relativeParts.forEach((part, index) => { + if (part === '.') { + rootParts.pop() + absoluteParts.shift() + } + + if (part === '..') { + index === 0 ? rootParts.splice(-2, 2) : rootParts.pop() + absoluteParts.shift() + } + }) + + return rootParts.concat(absoluteParts).join('/') + } + + return relativePath +}