Skip to content

Commit

Permalink
fix: 优化路由接口以及修复onResize回调参数缺失的字段
Browse files Browse the repository at this point in the history
  • Loading branch information
guoenxuan committed Dec 29, 2023
1 parent 13cf702 commit 84c6ba8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/taro-components/types/PageMeta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ declare namespace PageMetaProps {
windowWidth: number
/** 窗口高度 */
windowHeight: number
/** 屏幕宽度 */
screenWidth?: number
/** 屏幕高度 */
screenHeight?: number
}
interface onScrollEventDetail {
scrollTop: number
Expand Down
20 changes: 19 additions & 1 deletion packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
if ('url' in option) {
const pathPieces = processNavigateUrl(option)
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` }
fail?.(res)
complete?.(res)
if (fail || complete) {
return resolve(res)
} else {
return reject(res)
}
}
}
if (method === 'navigateTo') {
history.push(pathPieces, state)
} else if (method === 'redirectTo' || method === 'switchTab') {
Expand All @@ -74,7 +88,11 @@ async function navigate (option: Option | NavigateBackOption, method: MethodName
const res = { errMsg: `${method}:fail ${error.message || error}` }
fail?.(res)
complete?.(res)
reject(res)
if (fail || complete) {
return resolve(res)
} else {
return reject(res)
}
}
})
}
Expand Down
18 changes: 12 additions & 6 deletions packages/taro-router/src/events/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ export function bindPageResize (page: PageInstance) {
pageResizeFn && window.removeEventListener('resize', pageResizeFn)

pageResizeFn = function () {
page.onResize && page.onResize({
size: {
windowHeight: window.innerHeight,
windowWidth: window.innerWidth
}
})
if (page.onResize) {
const mediaQuery = window.matchMedia('(orientation: portrait)')
page.onResize({
deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
size: {
windowHeight: window.innerHeight,
windowWidth: window.innerWidth,
screenHeight: window.screen.height,
screenWidth: window.screen.width,
}
})
}
}

window.addEventListener('resize', pageResizeFn, false)
Expand Down
2 changes: 2 additions & 0 deletions packages/taro/types/taro.lifecycle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ declare module './index' {
size: {
windowWidth: number
windowHeight: number
screenWidth?: number
screenHeight?: number
}
}

Expand Down

0 comments on commit 84c6ba8

Please sign in to comment.