Skip to content

Commit

Permalink
fix(#15296): 修复路由有 basename 的情况下navigate 报错
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEJIA-LIU committed Mar 5, 2024
1 parent 5cd06ff commit 5898761
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
5 changes: 2 additions & 3 deletions packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
const state = { timestamp: Date.now() }
if (pathPieces.pathname) {
const originPath = routesAlias.getOrigin(pathPieces.pathname)
const pagePath = originPath.startsWith('/') ? originPath.substring(1) : originPath
if (!RouterConfig.pages.includes(pagePath)) {
const res = { errMsg: `${method}:fail page ${pagePath} is not found` }
if (!RouterConfig.isPage(addLeadingSlash(originPath))) {
const res = { errMsg: `${method}:fail page ${originPath} is not found` }
fail?.(res)
complete?.(res)
if (fail || complete) {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-router/src/history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, createBrowserHistory, createHashHistory } from 'history'

Check failure on line 1 in packages/taro-router/src/history.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Run autofix to sort these imports!

Check failure on line 1 in packages/taro-router/src/history.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Run autofix to sort these imports!

Check failure on line 1 in packages/taro-router/src/history.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Run autofix to sort these imports!

Check failure on line 1 in packages/taro-router/src/history.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Run autofix to sort these imports!

import { addLeadingSlash } from '@tarojs/runtime'
import { RouterConfig } from './router'

import type { IH5RouterConfig } from '@tarojs/taro/types/compile'
Expand Down Expand Up @@ -28,7 +28,7 @@ class MpaHistory implements History {

parseUrl (to: Partial<Path>) {
let url = to.pathname || ''
if (RouterConfig.isPage(url)) {
if (RouterConfig.isPage(addLeadingSlash(url))) {
url += '.html'
}
if (to.search) {
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 { addLeadingSlash } from '@tarojs/runtime'
import { addLeadingSlash, stripBasename } from '@tarojs/runtime'

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

Expand Down Expand Up @@ -28,6 +28,7 @@ export class RouterConfig {
static get customRoutes () { return this.router.customRoutes || {} }

static isPage (url = '') {
return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1
if (this.router.basename) url = stripBasename(url, this.router.basename)
return this.pages.findIndex(e => addLeadingSlash(e) === addLeadingSlash(url)) !== -1
}
}

0 comments on commit 5898761

Please sign in to comment.