diff --git a/packages/taro-router/src/api.ts b/packages/taro-router/src/api.ts index aff9190d8742..e4933c0f1c1e 100644 --- a/packages/taro-router/src/api.ts +++ b/packages/taro-router/src/api.ts @@ -60,21 +60,18 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName try { if ('url' in option) { - const pathPieces = processNavigateUrl(option) - const state = { timestamp: Date.now() } - if (pathPieces.pathname) { - const originPath = routesAlias.getOrigin(pathPieces.pathname) - if (!RouterConfig.isPage(addLeadingSlash(originPath)) && !RouterConfig.isPage(addLeadingSlash(pathPieces.pathname))) { - const res = { errMsg: `${method}:fail page ${originPath} is not found` } - fail?.(res) - complete?.(res) - if (fail || complete) { - return resolve(res) - } else { - return reject(res) - } + if (!RouterConfig.isPage(addLeadingSlash(option.url))) { + const res = { errMsg: `${method}:fail page ${option.url} is not found` } + fail?.(res) + complete?.(res) + if (fail || complete) { + return resolve(res) + } else { + return reject(res) } } + const pathPieces = processNavigateUrl(option) + const state = { timestamp: Date.now() } if (method === 'navigateTo') { history.push(pathPieces, state) } else if (method === 'redirectTo' || method === 'switchTab') { diff --git a/packages/taro-router/src/router/index.ts b/packages/taro-router/src/router/index.ts index 0e57d69a58e8..fb6de12c3b9d 100644 --- a/packages/taro-router/src/router/index.ts +++ b/packages/taro-router/src/router/index.ts @@ -1,4 +1,4 @@ -import { prependBasename } from '../history' +import { addLeadingSlash } from '@tarojs/runtime' import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router' @@ -27,7 +27,8 @@ export class RouterConfig { static get customRoutes () { return this.router.customRoutes || {} } + // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中 static isPage (url = '') { - return this.pages.findIndex(e => prependBasename(e) === url) !== -1 + return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1 } } diff --git a/packages/taro-router/src/router/navigation-bar.ts b/packages/taro-router/src/router/navigation-bar.ts index 8830d8ccbf34..09b0bd903b7a 100644 --- a/packages/taro-router/src/router/navigation-bar.ts +++ b/packages/taro-router/src/router/navigation-bar.ts @@ -41,7 +41,7 @@ export default class NavigationBarHandler { } private toHomeFn () { - reLaunch({ url: this.pageContext.homePage }) + reLaunch({ url: this.pageContext.originHomePage }) } private backFn () { diff --git a/packages/taro-router/src/router/page.ts b/packages/taro-router/src/router/page.ts index 7d4abbd0a195..27652a824f21 100644 --- a/packages/taro-router/src/router/page.ts +++ b/packages/taro-router/src/router/page.ts @@ -25,10 +25,12 @@ export default class PageHandler { protected navigationBarHandler: NavigationBarHandler public homePage: string + public originHomePage: string constructor (config: SpaRouterConfig, public history: History) { this.config = config this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath) + this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename this.mount() this.navigationBarHandler = new NavigationBarHandler(this) }