diff --git a/packages/taro-platform-harmony/src/runtime-framework/react/page.ts b/packages/taro-platform-harmony/src/runtime-framework/react/page.ts index c23c3abe1383..34c3993f30d8 100644 --- a/packages/taro-platform-harmony/src/runtime-framework/react/page.ts +++ b/packages/taro-platform-harmony/src/runtime-framework/react/page.ts @@ -88,6 +88,7 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? let pageElement: any = null let unmounting = false let prepareMountList: (() => void)[] = [] + let prepareLoadList: (() => void)[] = [] function setCurrentRouter (page) { const router = page.route || page.__route__ || page.$taroPath @@ -111,7 +112,12 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? const page = { [ONLOAD] (options: Readonly> = {}, cb?: (...args: any[]) => any) { hasLoaded = new Promise(resolve => { loadResolver = resolve }) - + hasLoaded.then(() => { + if (prepareLoadList.length) { + prepareLoadList.forEach(fn => fn()) + prepareLoadList = [] + } + }) Current.page = this as any // this.$taroPath 是页面唯一标识 @@ -218,7 +224,11 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? page[lifecycle] = function () { const exec = () => safeExecute(this.$taroPath, lifecycle, ...arguments) if (isDefer) { - hasLoaded.then(exec) + if (hasLoaded) { + hasLoaded.then(exec) + } else { + prepareLoadList.push(exec) + } } else { return exec() } diff --git a/packages/taro-platform-harmony/src/runtime-framework/solid/page.ts b/packages/taro-platform-harmony/src/runtime-framework/solid/page.ts index ec18c34157f3..caafc10e170d 100644 --- a/packages/taro-platform-harmony/src/runtime-framework/solid/page.ts +++ b/packages/taro-platform-harmony/src/runtime-framework/solid/page.ts @@ -88,6 +88,7 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? let pageElement: any = null let unmounting = false let prepareMountList: (() => void)[] = [] + let prepareLoadList: (() => void)[] = [] function setCurrentRouter (page) { const router = page.route || page.__route__ || page.$taroPath @@ -111,7 +112,12 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? const page = { [ONLOAD] (options: Readonly> = {}, cb?: (...args: any[]) => any) { hasLoaded = new Promise(resolve => { loadResolver = resolve }) - + hasLoaded.then(() => { + if (prepareLoadList.length) { + prepareLoadList.forEach(fn => fn()) + prepareLoadList = [] + } + }) Current.page = this as any // this.$taroPath 是页面唯一标识 @@ -217,7 +223,11 @@ export function createPageConfig (component: any, pageName?: string, pageConfig? page[lifecycle] = function () { const exec = () => safeExecute(this.$taroPath, lifecycle, ...arguments) if (isDefer) { - hasLoaded.then(exec) + if (hasLoaded) { + hasLoaded.then(exec) + } else { + prepareLoadList.push(exec) + } } else { return exec() } diff --git a/packages/taro-runtime/src/dsl/common.ts b/packages/taro-runtime/src/dsl/common.ts index 8bb109b75b5a..7a104d5a1db6 100644 --- a/packages/taro-runtime/src/dsl/common.ts +++ b/packages/taro-runtime/src/dsl/common.ts @@ -103,6 +103,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco let pageElement: TaroRootElement | null = null let unmounting = false let prepareMountList: (() => void)[] = [] + let prepareLoadList: (() => void)[] = [] function setCurrentRouter (page: MpInstance) { const router = process.env.TARO_PLATFORM === 'web' ? page.$taroPath : page.route || page.__route__ || page.$taroPath @@ -123,7 +124,12 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco const config: PageInstance = { [ONLOAD] (this: MpInstance, options: Readonly> = {}, cb?: TFunc) { hasLoaded = new Promise(resolve => { loadResolver = resolve }) - + hasLoaded.then(() => { + if (prepareLoadList.length) { + prepareLoadList.forEach(fn => fn()) + prepareLoadList = [] + } + }) perf.start(PAGE_INIT) Current.page = this as any @@ -246,7 +252,11 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco config[lifecycle] = function () { const exec = () => safeExecute(this.$taroPath, lifecycle, ...arguments) if (isDefer) { - hasLoaded.then(exec) + if (hasLoaded) { + hasLoaded.then(exec) + } else { + prepareLoadList.push(exec) + } } else { return exec() }