Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: 【发行为混合分包】TypeError: t.$callHook is not a function
1. 当发行为混合分包的时候,uniapp 会调用 initCreateSubpackageApp 方法

2. initCreateSubpackageApp 里调用 parseApp ,并在 onLaunch 时候进行 initBaseInstance

3. initCreateSubpackageApp 里调用 parseApp 后同步执行 `vm.$.ctx.$scope = app;`

4. initBaseInstance 在 onLaunch 会进行 `if (this.$vm && ctx.$scope) {return;}` 阻断,如果通过则执行 `ctx.$hasHook = hasHook;  ctx.$callHook = callHook;` 

问题出在 onLaunch 是异步的,导致 4 的流程阻断,没有执行 $callHook 赋值,最终导致 `initAppLifecycle` 中的 `vm.$callHook` 为 undefined
  • Loading branch information
viccici authored and chouchouji committed Aug 25, 2025
commit bbd003385bea85d39b82da30bc1fb2c77e295f29
2 changes: 2 additions & 0 deletions packages/uni-mp-core/src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) {
})
if (!app) return
;(vm.$ as any).ctx.$scope = app
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning '$hasHook' and '$callHook' synchronously resolves the async race condition that previously resulted in 't.$callHook is not a function'. Consider adding a comment explaining why these assignments are done here for future maintainability.

Suggested change
;(vm.$ as any).ctx.$scope = app
;(vm.$ as any).ctx.$scope = app
// Assigning `$hasHook` and `$callHook` synchronously is critical to prevent
// an async race condition that could result in `t.$callHook is not a function`.
// Do not modify the order or timing of these assignments without understanding
// the potential impact on app lifecycle hooks.

Copilot uses AI. Check for mistakes.

(vm.$ as any).ctx.$hasHook = hasHook
(vm.$ as any).ctx.$callHook = callHook
const globalData = app.globalData
if (globalData) {
Object.keys(appOptions.globalData).forEach((name) => {
Expand Down