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 } }