From bbd003385bea85d39b82da30bc1fb2c77e295f29 Mon Sep 17 00:00:00 2001 From: Ronny Date: Thu, 11 Apr 2024 10:10:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E3=80=90=E5=8F=91=E8=A1=8C=E4=B8=BA?= =?UTF-8?q?=E6=B7=B7=E5=90=88=E5=88=86=E5=8C=85=E3=80=91TypeError:=20t.$ca?= =?UTF-8?q?llHook=20is=20not=20a=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/uni-mp-core/src/runtime/app.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/uni-mp-core/src/runtime/app.ts b/packages/uni-mp-core/src/runtime/app.ts index 06c28183d23..4c4a674e3c6 100644 --- a/packages/uni-mp-core/src/runtime/app.ts +++ b/packages/uni-mp-core/src/runtime/app.ts @@ -123,6 +123,8 @@ export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) { }) if (!app) return ;(vm.$ as any).ctx.$scope = app + (vm.$ as any).ctx.$hasHook = hasHook + (vm.$ as any).ctx.$callHook = callHook const globalData = app.globalData if (globalData) { Object.keys(appOptions.globalData).forEach((name) => { From 3b4248252f5c971427aa851290eb6ce0ab27f4ad Mon Sep 17 00:00:00 2001 From: chouchouji <1305974212@qq.com> Date: Mon, 25 Aug 2025 20:09:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(mp):=20=E4=BF=AE=E5=A4=8D=E5=8F=91?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E6=B7=B7=E5=90=88=E5=8F=91=E5=8C=85=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-core/src/runtime/app.ts | 6 +++--- packages/uni-mp-core/src/runtime/componentInstance.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/uni-mp-core/src/runtime/app.ts b/packages/uni-mp-core/src/runtime/app.ts index 4c4a674e3c6..8a21abc7e6d 100644 --- a/packages/uni-mp-core/src/runtime/app.ts +++ b/packages/uni-mp-core/src/runtime/app.ts @@ -6,7 +6,7 @@ import { ref, } from 'vue' -import { initBaseInstance } from './componentInstance' +import { callHook, hasHook, initBaseInstance } from './componentInstance' import { initHooks, initUnknownHooks } from './componentHooks' import { getLocaleLanguage } from '../runtime/util' @@ -123,8 +123,8 @@ export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) { }) if (!app) return ;(vm.$ as any).ctx.$scope = app - (vm.$ as any).ctx.$hasHook = hasHook - (vm.$ as any).ctx.$callHook = callHook + ;(vm.$ as any).ctx.$hasHook = hasHook + ;(vm.$ as any).ctx.$callHook = callHook const globalData = app.globalData if (globalData) { Object.keys(appOptions.globalData).forEach((name) => { diff --git a/packages/uni-mp-core/src/runtime/componentInstance.ts b/packages/uni-mp-core/src/runtime/componentInstance.ts index 8a1e1407e08..00fddfd01a4 100644 --- a/packages/uni-mp-core/src/runtime/componentInstance.ts +++ b/packages/uni-mp-core/src/runtime/componentInstance.ts @@ -187,7 +187,7 @@ export function initMocks( }) } -function hasHook(this: ComponentPublicInstance, name: string) { +export function hasHook(this: ComponentPublicInstance, name: string) { const hooks = (this.$ as any)[name] if (hooks && hooks.length) { return true @@ -195,7 +195,11 @@ function hasHook(this: ComponentPublicInstance, name: string) { return false } -function callHook(this: ComponentPublicInstance, name: string, args?: unknown) { +export function callHook( + this: ComponentPublicInstance, + name: string, + args?: unknown +) { if (name === 'mounted') { callHook.call(this, 'bm') // beforeMount this.$.isMounted = true