Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复多自定义路由的时候跳转的bug #15725

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
pathPieces.pathname = parts.join('/')
}

// 确保是 / 开头的路径

Check warning on line 34 in packages/taro-router/src/api.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/api.ts#L34

Added line #L34 was not covered by tests
pathPieces.pathname = addLeadingSlash(pathPieces.pathname)

// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
if (!pathPieces.search) pathPieces.search = ''

Expand Down Expand Up @@ -66,9 +69,11 @@

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

Check warning on line 74 in packages/taro-router/src/api.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/api.ts#L74

Added line #L74 was not covered by tests
const originPath = routesAlias.getOrigin(pathPieces.pathname)
if (!RouterConfig.isPage(pathPieces.pathname) && !RouterConfig.isPage(originPath)) {

Check warning on line 76 in packages/taro-router/src/api.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/api.ts#L76

Added line #L76 was not covered by tests
const res = { errMsg: `${method}:fail page ${option.url} is not found` }
fail?.(res)
complete?.(res)
Expand Down
10 changes: 5 additions & 5 deletions packages/taro-router/src/router/navigation-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
}

setCacheValue () {
const currentPage = this.pageContext.currentPage
const currentPage = this.pageContext.originPathname

Check warning on line 93 in packages/taro-router/src/router/navigation-bar.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/router/navigation-bar.ts#L93

Added line #L93 was not covered by tests
if (typeof this.cache[currentPage] !== 'object') {
this.cache[currentPage] = {}
}
Expand Down Expand Up @@ -118,7 +118,7 @@

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
Expand All @@ -142,7 +142,7 @@
setNavigationBarBackground (backgroundColor?: string) {
if (!this.navigationBarElement) return

const currentPage = this.pageContext.currentPage
const currentPage = this.pageContext.originPathname

Check warning on line 145 in packages/taro-router/src/router/navigation-bar.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/router/navigation-bar.ts#L145

Added line #L145 was not covered by tests
let color
if (typeof backgroundColor === 'string') {
color = backgroundColor
Expand All @@ -164,7 +164,7 @@
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
Expand All @@ -184,7 +184,7 @@
}

setTitle (title?) {
const currentPage = this.pageContext.currentPage
const currentPage = this.pageContext.originPathname
let proceedTitle
if (typeof title === 'string') {
proceedTitle = title
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-router/src/router/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@

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)

Check warning on line 77 in packages/taro-router/src/router/page.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-router/src/router/page.ts#L77

Added line #L77 was not covered by tests
})
}

Expand Down
Loading