diff --git a/packages/taro-router/src/api.ts b/packages/taro-router/src/api.ts index fbbdbbf433f6..738bea49f147 100644 --- a/packages/taro-router/src/api.ts +++ b/packages/taro-router/src/api.ts @@ -31,6 +31,9 @@ function processNavigateUrlWithRelativePath (option: Option): Partial { pathPieces.pathname = parts.join('/') } + // 确保是 / 开头的路径 + pathPieces.pathname = addLeadingSlash(pathPieces.pathname) + // hack fix history v5 bug: https://github.com/remix-run/history/issues/814 if (!pathPieces.search) pathPieces.search = '' @@ -66,9 +69,11 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName try { if ('url' in option) { - let pathPieces = processNavigateUrlWithRelativePath(option) // Note: 因为 RouterConfig.isPage 方法不对 customRoutes 和 basename 进行处理,所以这里也不处理 - if (!RouterConfig.isPage(addLeadingSlash(pathPieces.pathname))) { + let pathPieces = processNavigateUrlWithRelativePath(option) + // Note: 这里还有判断一种情况,可能是直接跳转到自定义路由的页面,所以需要把 customRoute 转化为页面 router + const originPath = routesAlias.getOrigin(pathPieces.pathname) + if (!RouterConfig.isPage(pathPieces.pathname) && !RouterConfig.isPage(originPath)) { const res = { errMsg: `${method}:fail page ${option.url} is not found` } fail?.(res) complete?.(res) diff --git a/packages/taro-router/src/router/navigation-bar.ts b/packages/taro-router/src/router/navigation-bar.ts index 09b0bd903b7a..a17e26c071c0 100644 --- a/packages/taro-router/src/router/navigation-bar.ts +++ b/packages/taro-router/src/router/navigation-bar.ts @@ -90,7 +90,7 @@ export default class NavigationBarHandler { } setCacheValue () { - const currentPage = this.pageContext.currentPage + const currentPage = this.pageContext.originPathname if (typeof this.cache[currentPage] !== 'object') { this.cache[currentPage] = {} } @@ -118,7 +118,7 @@ export default class NavigationBarHandler { setNavigationLoading (show?: boolean) { if (!this.navigationBarElement) return - const currentPage = this.pageContext.currentPage + const currentPage = this.pageContext.originPathname let isShow if (typeof show === 'boolean') { isShow = show @@ -142,7 +142,7 @@ export default class NavigationBarHandler { setNavigationBarBackground (backgroundColor?: string) { if (!this.navigationBarElement) return - const currentPage = this.pageContext.currentPage + const currentPage = this.pageContext.originPathname let color if (typeof backgroundColor === 'string') { color = backgroundColor @@ -164,7 +164,7 @@ export default class NavigationBarHandler { setNavigationBarTextStyle (fontColor?: string) { if (!this.navigationBarElement) return - const currentPage = this.pageContext.currentPage + const currentPage = this.pageContext.originPathname let color if (typeof fontColor === 'string') { color = fontColor @@ -184,7 +184,7 @@ export default class NavigationBarHandler { } setTitle (title?) { - const currentPage = this.pageContext.currentPage + const currentPage = this.pageContext.originPathname let proceedTitle if (typeof title === 'string') { proceedTitle = title diff --git a/packages/taro-router/src/router/page.ts b/packages/taro-router/src/router/page.ts index 27652a824f21..29df44d7706f 100644 --- a/packages/taro-router/src/router/page.ts +++ b/packages/taro-router/src/router/page.ts @@ -66,14 +66,15 @@ export default class PageHandler { set pathname (p) { this.router.pathname = p } get pathname () { return this.router.pathname } + // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename + get originPathname () { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))) } get basename () { return this.router.basename || '' } get pageConfig () { - const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename)) const homePage = addLeadingSlash(this.homePage) return this.routes.find(r => { const pagePath = addLeadingSlash(r.path) - return [pagePath, homePage].includes(routePath) || routesAlias.getConfig(pagePath)?.includes(routePath) + return [pagePath, homePage].includes(this.originPathname) }) }