Skip to content

Commit 01b2b5b

Browse files
committed
feat(x-app): 页面跳转时,触发 dialogPage hide 生命周期 (issues/detail?id=26530)
1 parent 11579ec commit 01b2b5b

6 files changed

Lines changed: 66 additions & 8 deletions

File tree

packages/shims-uni-app.d.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ declare class UniNormalPageImpl implements UniPage {
592592
readonly width: number
593593
readonly height: number
594594
readonly statusBarHeight: number
595-
getParentPage: () => UniPage | null
596-
getParentPageByJS: () => UniPage | null
595+
getParentPage(): UniPage | null
596+
getParentPageByJS(): UniPage | null
597597
getDialogPages(): UniDialogPage[]
598598
$getSystemDialogPages(): UniDialogPage[]
599599
__$$getSystemDialogPages(): UniDialogPage[]
@@ -610,6 +610,17 @@ declare class UniNormalPageImpl implements UniPage {
610610
getAndroidActivity(): Activity | null
611611
exitFullscreen(options: ExitFullscreenOptions | null): void
612612
createElement(tagName: string): UniElement
613+
querySelector(selector: string): UniElement | null
614+
querySelectorAll(selector: string): UniElement[]
615+
onLayoutChange(): number
616+
offLayoutChange(): void
617+
onRenderChange(): number
618+
offRenderChange(): void
619+
onTouchStart(): number
620+
offTouchStart(): void
621+
onTouchEnd(): number
622+
offTouchEnd(): void
623+
takeSnapshot(): void
613624
}
614625

615626
declare class UniDialogPageImpl implements UniPage {
@@ -624,8 +635,8 @@ declare class UniDialogPageImpl implements UniPage {
624635
readonly width: number
625636
readonly height: number
626637
readonly statusBarHeight: number
627-
getParentPage: () => UniPage | null
628-
getParentPageByJS: () => UniPage | null
638+
getParentPage(): UniPage | null
639+
getParentPageByJS(): UniPage | null
629640
getDialogPages(): UniDialogPage[]
630641
$getSystemDialogPages(): UniDialogPage[]
631642
__$$getSystemDialogPages(): UniDialogPage[]
@@ -639,9 +650,20 @@ declare class UniDialogPageImpl implements UniPage {
639650
getAndroidView(): null
640651
getIOSView(): null
641652
getHTMLElement(): null
642-
getAndroidActivity: () => Activity | null
653+
getAndroidActivity(): Activity | null
643654
exitFullscreen(options: ExitFullscreenOptions | null): void
644655
createElement(tagName: string): UniElement
656+
querySelector(selector: string): UniElement | null
657+
querySelectorAll(selector: string): UniElement[]
658+
onLayoutChange(): number
659+
offLayoutChange(): void
660+
onRenderChange(): number
661+
offRenderChange(): void
662+
onTouchStart(): number
663+
offTouchStart(): void
664+
onTouchEnd(): number
665+
offTouchEnd(): void
666+
takeSnapshot(): void
645667
$component: any | null
646668
$disableEscBack: boolean
647669
$triggerParentHide: boolean

packages/uni-app-harmony/src/service/api/route/navigateBack.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getCurrentPage,
1111
initI18nAppMsgsOnce,
1212
invokeHook,
13+
invokeLastDialogPageHookByUniPage,
1314
useI18n,
1415
} from '@dcloudio/uni-core'
1516
import { ON_BACK_PRESS, ON_SHOW } from '@dcloudio/uni-shared'
@@ -131,6 +132,10 @@ function back(
131132
setStatusBarStyle()
132133
// 前一个页面触发 onShow
133134
invokeHook(ON_SHOW)
135+
invokeLastDialogPageHookByUniPage(
136+
getCurrentPage() as unknown as UniPage,
137+
ON_SHOW
138+
)
134139
}
135140

136141
_backWebview(currentPage, backPage)

packages/uni-app-harmony/src/service/api/route/navigateTo.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { EventChannel, ON_HIDE, parseUrl } from '@dcloudio/uni-shared'
2-
import { invokeHook } from '@dcloudio/uni-core'
2+
import {
3+
getCurrentPage,
4+
invokeHook,
5+
invokeLastDialogPageHookByUniPage,
6+
} from '@dcloudio/uni-core'
37
import {
48
API_NAVIGATE_TO,
59
type API_TYPE_NAVIGATE_TO,
@@ -67,6 +71,10 @@ function _navigateTo({
6771
}: NavigateToOptions): Promise<void | { eventChannel: EventChannel }> {
6872
// 当前页面触发 onHide
6973
invokeHook(ON_HIDE)
74+
invokeLastDialogPageHookByUniPage(
75+
getCurrentPage() as unknown as UniPage,
76+
ON_HIDE
77+
)
7078
const eventChannel = new EventChannel(getWebviewId() + 1, events)
7179
return new Promise((resolve) => {
7280
showWebview(

packages/uni-app-plus/src/x/api/route/navigateBack.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
NavigateBackProtocol,
77
defineAsyncApi,
88
} from '@dcloudio/uni-api'
9-
import { getCurrentPage, invokeHook } from '@dcloudio/uni-core'
9+
import {
10+
getCurrentPage,
11+
invokeHook,
12+
invokeLastDialogPageHookByUniPage,
13+
} from '@dcloudio/uni-core'
1014
import {
1115
ON_BACK_PRESS,
1216
ON_LAST_PAGE_BACK_PRESS,
@@ -128,6 +132,10 @@ function back(
128132
.forEach((page) => removePage(page as ComponentPublicInstance))
129133
// 前一个页面触发 onShow
130134
invokeHook(ON_SHOW)
135+
invokeLastDialogPageHookByUniPage(
136+
getCurrentPage() as unknown as UniPage,
137+
ON_SHOW
138+
)
131139
setStatusBarStyle()
132140
})
133141
}

packages/uni-app-plus/src/x/api/route/navigateTo.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { EventChannel, ON_HIDE, parseUrl } from '@dcloudio/uni-shared'
2-
import { getCurrentPage, getRouteMeta, invokeHook } from '@dcloudio/uni-core'
2+
import {
3+
getCurrentPage,
4+
getRouteMeta,
5+
invokeHook,
6+
invokeLastDialogPageHookByUniPage,
7+
} from '@dcloudio/uni-core'
38
import {
49
API_NAVIGATE_TO,
510
type API_TYPE_NAVIGATE_TO,
@@ -82,6 +87,7 @@ function _navigateTo({
8287
invokeBeforeRouteHooks(currentRouteType)
8388
// 当前页面触发 onHide
8489
invokeHook(ON_HIDE)
90+
currentPage && invokeLastDialogPageHookByUniPage(currentPage.$page, ON_HIDE)
8591
const eventChannel = new EventChannel(getWebviewId() + 1, events)
8692
return new Promise((resolve) => {
8793
setTimeout(() => {

packages/uni-core/src/helpers/dialogPage.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export function invokeNewDialogPageHook(page: UniDialogPage, hook: string) {
133133
}
134134

135135
export function getPageInstanceByChild(child: ComponentInternalInstance) {
136+
if (__PLATFORM__ === 'app') {
137+
// @ts-expect-error
138+
return child.ctx.$basePage
139+
}
136140
let pageInstance: ComponentInternalInstance | null = child
137141
while (pageInstance && pageInstance.type?.name !== 'Page') {
138142
pageInstance = pageInstance.parent
@@ -145,6 +149,11 @@ export const SYSTEM_DIALOG_TAG = 'systemDialog'
145149

146150
export function isDialogPageInstance(vm: ComponentInternalInstance | null) {
147151
if (!vm) return false
152+
if (__PLATFORM__ === 'app') {
153+
// @ts-expect-error
154+
return vm.openType === 'openDialogPage'
155+
}
156+
148157
return isNormalDialogPageInstance(vm) || isSystemDialogPageInstance(vm)
149158
}
150159
export function isNormalDialogPageInstance(vm: ComponentInternalInstance) {

0 commit comments

Comments
 (0)