Skip to content

Commit 7b72c48

Browse files
committed
fix(x-web): 修复非栈顶页面打开 dialogPage 触发 onShow 生命周期问题 (issues/detail?id=26547)
1 parent cd1f74f commit 7b72c48

7 files changed

Lines changed: 56 additions & 33 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { UniDialogPage } from '@dcloudio/uni-app-x/types/page'
22
import { ON_HIDE, ON_SHOW } from '@dcloudio/uni-shared'
33
import { invokeHook } from './hook'
4+
import { getCurrentPage } from '@dcloudio/uni-core'
5+
import type { ComponentInternalInstance } from 'vue'
46

57
export const SYSTEM_DIALOG_PAGE_PATH_STARTER = 'uni:'
68
export const SYSTEM_DIALOG_ACTION_SHEET_PAGE_PATH = 'uni:actionSheet'
@@ -116,3 +118,38 @@ export function invokeLastDialogPageHookByUniPage(
116118
invokeHook(lastDialogPage.vm, hook)
117119
}
118120
}
121+
122+
export function invokeNewDialogPageHook(page: UniDialogPage, hook: string) {
123+
let shouldInvoke = false
124+
const currentPage = getCurrentPage() as unknown as UniPage
125+
if (isSystemDialogPage(page)) {
126+
const systemDialogPages = getSystemDialogPages(currentPage)
127+
shouldInvoke = systemDialogPages.includes(page as UniDialogPage)
128+
} else {
129+
const dialogPages = currentPage.getDialogPages()
130+
shouldInvoke = dialogPages.includes(page as UniDialogPage)
131+
}
132+
shouldInvoke && invokeHook(page.vm, hook)
133+
}
134+
135+
export function getPageInstanceByChild(child: ComponentInternalInstance) {
136+
let pageInstance: ComponentInternalInstance | null = child
137+
while (pageInstance && pageInstance.type?.name !== 'Page') {
138+
pageInstance = pageInstance.parent
139+
}
140+
return pageInstance
141+
}
142+
143+
export const DIALOG_TAG = 'dialog'
144+
export const SYSTEM_DIALOG_TAG = 'systemDialog'
145+
146+
export function isDialogPageInstance(vm: ComponentInternalInstance | null) {
147+
if (!vm) return false
148+
return isNormalDialogPageInstance(vm) || isSystemDialogPageInstance(vm)
149+
}
150+
export function isNormalDialogPageInstance(vm: ComponentInternalInstance) {
151+
return vm.attrs['data-type'] === DIALOG_TAG
152+
}
153+
export function isSystemDialogPageInstance(vm: ComponentInternalInstance) {
154+
return vm.attrs['data-type'] === SYSTEM_DIALOG_TAG
155+
}

packages/uni-h5/src/framework/components/page/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
isDialogPageInstance,
3333
isNormalDialogPageInstance,
3434
isSystemDialogPageInstance,
35-
} from '../../../x/framework/helpers/utils'
35+
} from '@dcloudio/uni-core'
3636
//#endif
3737

3838
export default /*#__PURE__*/ defineSystemComponent({

packages/uni-h5/src/framework/setup/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ import {
4949
onPageShow,
5050
} from './page'
5151
import { usePageMeta, usePageRoute } from './provide'
52-
import {
53-
getEnterOptions,
54-
getPageInstanceByChild,
55-
initLaunchOptions,
56-
} from './utils'
52+
import { getEnterOptions, initLaunchOptions } from './utils'
5753
import type { RouteLocationNormalizedLoaded } from 'vue-router'
5854
import { useRouter } from 'vue-router'
5955
import { handleBeforeEntryPageRoutes } from '../../service/api/route/utils'
6056
import { updateCurPageCssVar } from '../../helpers/cssVar'
6157
//#if _X_
62-
import { isDialogPageInstance } from '../../x/framework/helpers/utils'
58+
import {
59+
getPageInstanceByChild,
60+
isDialogPageInstance,
61+
} from '@dcloudio/uni-core'
6362
import { useBackgroundColorContent } from '../../x/framework/setup/page'
6463
//#endif
6564
import { getPageProxyId } from '@dcloudio/uni-core'

packages/uni-h5/src/framework/setup/utils.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,3 @@ export function initLaunchOptions({
2828
extend(enterOptions, launchOptions)
2929
return extend({}, launchOptions)
3030
}
31-
32-
export function getPageInstanceByChild(child: ComponentInternalInstance) {
33-
let pageInstance: ComponentInternalInstance | null = child
34-
while (pageInstance && pageInstance.type?.name !== 'Page') {
35-
pageInstance = pageInstance.parent
36-
}
37-
return pageInstance
38-
}

packages/uni-h5/src/x/framework/helpers/utils.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/uni-h5/src/x/framework/setup/page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
normalizeRouteKey,
2727
} from '../../../framework/setup/page'
2828
import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router'
29-
import { isDialogPageInstance } from '../helpers/utils'
29+
import { isDialogPageInstance } from '@dcloudio/uni-core'
3030
import type { UniSafeAreaInsets } from '@dcloudio/uni-app-x/types/native/UniSafeAreaInsets'
3131
import { normalizeStyles } from '@dcloudio/uni-shared'
3232

packages/uni-vue/src/componentOptions/hooks.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { invokeHook } from '@dcloudio/uni-core'
2+
//#if _X_
3+
import {
4+
getPageInstanceByChild,
5+
invokeNewDialogPageHook,
6+
isDialogPageInstance,
7+
} from '@dcloudio/uni-core'
8+
//#endif
29
import {
310
LINEFEED,
411
ON_LOAD,
@@ -7,8 +14,7 @@ import {
714
decodedQuery,
815
isUniLifecycleHook,
916
} from '@dcloudio/uni-shared'
10-
import { isArray, isFunction } from '@vue/shared'
11-
import { hasOwn } from '@vue/shared'
17+
import { hasOwn, isArray, isFunction } from '@vue/shared'
1218

1319
import type {
1420
ComponentInternalInstance,
@@ -87,6 +93,10 @@ export function initHooks(
8793
: (publicThis.$page as Page.PageInstance['$page'])
8894
if (!(__PLATFORM__ === 'app' && __X__ && $basePage?.meta.isTabBar)) {
8995
if ($basePage?.openType !== 'preloadPage') {
96+
if (__X__ && isDialogPageInstance(getPageInstanceByChild(instance))) {
97+
invokeNewDialogPageHook(publicThis.$page as UniDialogPage, ON_SHOW)
98+
return
99+
}
90100
invokeHook(publicThis, ON_SHOW)
91101
}
92102
}

0 commit comments

Comments
 (0)