From 58987618e2b7176bc400a292c22709a37c7b0500 Mon Sep 17 00:00:00 2001 From: liuzejia <790868516@qq.com> Date: Tue, 5 Mar 2024 20:27:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(#15296):=20=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E6=9C=89=20basename=20=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8Bnavigate=20=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-router/src/api.ts | 5 ++--- packages/taro-router/src/history.ts | 4 ++-- packages/taro-router/src/router/index.ts | 5 +++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/taro-router/src/api.ts b/packages/taro-router/src/api.ts index 217cc1195582..fb57563476fe 100644 --- a/packages/taro-router/src/api.ts +++ b/packages/taro-router/src/api.ts @@ -57,9 +57,8 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName const state = { timestamp: Date.now() } if (pathPieces.pathname) { const originPath = routesAlias.getOrigin(pathPieces.pathname) - const pagePath = originPath.startsWith('/') ? originPath.substring(1) : originPath - if (!RouterConfig.pages.includes(pagePath)) { - const res = { errMsg: `${method}:fail page ${pagePath} is not found` } + if (!RouterConfig.isPage(addLeadingSlash(originPath))) { + const res = { errMsg: `${method}:fail page ${originPath} is not found` } fail?.(res) complete?.(res) if (fail || complete) { diff --git a/packages/taro-router/src/history.ts b/packages/taro-router/src/history.ts index 485d34e30ce8..8ab4942c4be2 100644 --- a/packages/taro-router/src/history.ts +++ b/packages/taro-router/src/history.ts @@ -1,5 +1,5 @@ import { Action, createBrowserHistory, createHashHistory } from 'history' - +import { addLeadingSlash } from '@tarojs/runtime' import { RouterConfig } from './router' import type { IH5RouterConfig } from '@tarojs/taro/types/compile' @@ -28,7 +28,7 @@ class MpaHistory implements History { parseUrl (to: Partial) { let url = to.pathname || '' - if (RouterConfig.isPage(url)) { + if (RouterConfig.isPage(addLeadingSlash(url))) { url += '.html' } if (to.search) { diff --git a/packages/taro-router/src/router/index.ts b/packages/taro-router/src/router/index.ts index f3335b1c49f5..b99ffca1f0ab 100644 --- a/packages/taro-router/src/router/index.ts +++ b/packages/taro-router/src/router/index.ts @@ -1,4 +1,4 @@ -import { addLeadingSlash } from '@tarojs/runtime' +import { addLeadingSlash, stripBasename } from '@tarojs/runtime' import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router' @@ -28,6 +28,7 @@ export class RouterConfig { static get customRoutes () { return this.router.customRoutes || {} } static isPage (url = '') { - return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1 + if (this.router.basename) url = stripBasename(url, this.router.basename) + return this.pages.findIndex(e => addLeadingSlash(e) === addLeadingSlash(url)) !== -1 } }