Skip to content

Commit

Permalink
fix: TypeError: cannot read property 'then' of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nieheyong committed Dec 14, 2024
1 parent 8501202 commit 6245d54
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
14 changes: 12 additions & 2 deletions packages/taro-platform-harmony/src/runtime-framework/react/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -111,7 +112,12 @@ export function createPageConfig (component: any, pageName?: string, pageConfig?
const page = {
[ONLOAD] (options: Readonly<Record<string, unknown>> = {}, 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 是页面唯一标识
Expand Down Expand Up @@ -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()
}
Expand Down
14 changes: 12 additions & 2 deletions packages/taro-platform-harmony/src/runtime-framework/solid/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -111,7 +112,12 @@ export function createPageConfig (component: any, pageName?: string, pageConfig?
const page = {
[ONLOAD] (options: Readonly<Record<string, unknown>> = {}, 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 是页面唯一标识
Expand Down Expand Up @@ -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()
}
Expand Down
14 changes: 12 additions & 2 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -123,7 +124,12 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
const config: PageInstance = {
[ONLOAD] (this: MpInstance, options: Readonly<Record<string, unknown>> = {}, 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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 6245d54

Please sign in to comment.