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

fix4375 解决快应用中路由跳转API不支持相对路径的问题 #4503

Merged
merged 2 commits into from
Mar 26, 2020
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
20 changes: 17 additions & 3 deletions packages/taro-quickapp/src/api/router/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import router from '@system.router'

import appGlobal from '../../global'
import { addLeadingSlash, getUniqueKey } from '../../util'
import { cacheDataGet, cacheDataSet } from '../../data-cache'
Expand Down Expand Up @@ -47,7 +46,22 @@ function qappNavigate (options = {}, method = 'push') {
}
params = getUrlParams(url)
const markIndex = url.indexOf('?')
const parseUrl = addLeadingSlash(url.substr(0, markIndex >= 0 ? markIndex : url.length))
const componentPath = appGlobal.componentPath || ''
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

/pages/home/home里面引用../other/other

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

parseUrl会转为快应用支持的/pages/other/other

let parseUrl = url.substr(0, markIndex >= 0 ? markIndex : url.length)
const RelativeReg = /\.\.\//g
if (componentPath && RelativeReg.test(parseUrl)) {
//当前页面路径最后一级是文件,在计算路径时去除
var componentRootDir = componentPath.substr(0, componentPath.lastIndexOf('/'))
var pathArr = parseUrl.split('/')
//计算..出现的次数,每出现一次就往上一层
for(let path of pathArr) {
if (path === '..') {
componentRootDir = componentRootDir.substr(0, componentRootDir.lastIndexOf('/'))
}
}
parseUrl = componentRootDir + '/' + parseUrl.replace(RelativeReg, '')
}
parseUrl = addLeadingSlash(parseUrl)
appGlobal.taroRouterParamsCache = appGlobal.taroRouterParamsCache || {}
appGlobal.taroRouterParamsCache[parseUrl] = params

Expand All @@ -65,7 +79,7 @@ function qappNavigate (options = {}, method = 'push') {
}
try {
router[method]({
uri: url.substr(0, url.lastIndexOf('/')),
uri: parseUrl.substr(0, parseUrl.lastIndexOf('/')),
params
})
success && success(res)
Expand Down
1 change: 1 addition & 0 deletions packages/taro-quickapp/src/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export default function createComponent (ComponentClass, isPage) {
}
}
})
appGlobal.componentPath = isPage
addLeadingSlash(isPage) && cacheDataSet(addLeadingSlash(isPage), ComponentClass)
}
bindStaticFns(componentConf, ComponentClass)
Expand Down