提交 75d07614 编写于 作者: fxy060608's avatar fxy060608

fix(h5): onLoad

上级 2e1f2ac1
...@@ -33,13 +33,17 @@ import { initLaunchOptions, getEnterOptions } from './utils' ...@@ -33,13 +33,17 @@ import { initLaunchOptions, getEnterOptions } from './utils'
interface SetupComponentOptions { interface SetupComponentOptions {
init: (vm: ComponentPublicInstance) => void init: (vm: ComponentPublicInstance) => void
setup: (instance: ComponentInternalInstance) => Record<string, any> | void setup: (instance: ComponentInternalInstance) => Record<string, unknown> | void
afterSetup?: (
instance: ComponentInternalInstance,
query: Record<string, unknown>
) => void
before?: (comp: DefineComponent) => void before?: (comp: DefineComponent) => void
} }
function wrapperComponentSetup( function wrapperComponentSetup(
comp: DefineComponent, comp: DefineComponent,
{ init, setup, before }: SetupComponentOptions { init, setup, before, afterSetup }: SetupComponentOptions
) { ) {
before && before(comp) before && before(comp)
const oldSetup = comp.setup const oldSetup = comp.setup
...@@ -50,6 +54,9 @@ function wrapperComponentSetup( ...@@ -50,6 +54,9 @@ function wrapperComponentSetup(
if (oldSetup) { if (oldSetup) {
return oldSetup(query || props, ctx) return oldSetup(query || props, ctx)
} }
if (afterSetup) {
afterSetup(instance, query!)
}
} }
} }
...@@ -78,6 +85,11 @@ export function setupWindow(comp: any, id: number) { ...@@ -78,6 +85,11 @@ export function setupWindow(comp: any, id: number) {
export function setupPage(comp: any) { export function setupPage(comp: any) {
return setupComponent(comp, { return setupComponent(comp, {
init: initPage, init: initPage,
afterSetup(instance, query) {
// 因为 onLoad 是在 setup 执行过程中添加的,故需要放在 after 中执行
const { onLoad } = instance
onLoad && invokeArrayFns(onLoad, decodedQuery(query))
},
setup(instance) { setup(instance) {
instance.root = instance // 组件 root 指向页面 instance.root = instance // 组件 root 指向页面
const route = usePageRoute() const route = usePageRoute()
...@@ -89,12 +101,9 @@ export function setupPage(comp: any) { ...@@ -89,12 +101,9 @@ export function setupPage(comp: any) {
}) })
return route.query return route.query
} }
const pageMeta = usePageMeta() const pageMeta = usePageMeta()
// 放在 onMounted 中,可以保证子组件中监听的相关生命周期也可以触发,比如onShow,onPageScroll
const { onLoad } = instance
onLoad && invokeArrayFns(onLoad, decodedQuery(route.query))
onMounted(() => { onMounted(() => {
// 放在 onMounted 中,可以保证子组件中监听的相关生命周期也可以触发,比如onShow,onPageScroll
onPageShow(instance, pageMeta) onPageShow(instance, pageMeta)
const { onShow } = instance const { onShow } = instance
instance.__isVisible = true instance.__isVisible = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册