Skip to content

Commit

Permalink
fix: 修复路由判定逻辑 (#15658)
Browse files Browse the repository at this point in the history
Co-authored-by: liuzejia <liuzejia@SZMAC-FV0MR4G7.local>
  • Loading branch information
ZEJIA-LIU and liuzejia authored May 10, 2024
1 parent c76a103 commit 2854bba
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
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 @@ 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') {
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 @@ export default class NavigationBarHandler {
}

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

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

0 comments on commit 2854bba

Please sign in to comment.