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: 修复自定义路由的跳转问题 #15658

Merged
merged 3 commits into from
May 10, 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
23 changes: 10 additions & 13 deletions packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,18 @@

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))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZEJIA-LIU 如果 url 携带了参数会匹配不到

const res = { errMsg: `${method}:fail page ${option.url} is not found` }

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L64 was not covered by tests
fail?.(res)
complete?.(res)
if (fail || complete) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L67 was not covered by tests
return resolve(res)
} else {
return reject(res)
}
}
const pathPieces = processNavigateUrl(option)
const state = { timestamp: Date.now() }

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#L73-L74

Added lines #L73 - L74 were not covered by tests
if (method === 'navigateTo') {
history.push(pathPieces, state)
} else if (method === 'redirectTo' || method === 'switchTab') {
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-router/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prependBasename } from '../history'
import { addLeadingSlash } from '@tarojs/runtime'

import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router'

Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion packages/taro-router/src/router/navigation-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}

private toHomeFn () {
reLaunch({ url: this.pageContext.homePage })
reLaunch({ url: this.pageContext.originHomePage })

Check warning on line 44 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#L44

Added line #L44 was not covered by tests
}

private backFn () {
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-router/src/router/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading