From 0825d3087f9f39435838329c16adc2a7bfccd51d Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 12 Jul 2022 18:19:19 +0800 Subject: [PATCH] fix: do not set currentInstance in beforeCreate fix #12636 --- src/core/instance/init.ts | 2 +- src/core/instance/lifecycle.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/core/instance/init.ts b/src/core/instance/init.ts index 564dbf9fb..876c9ddbf 100644 --- a/src/core/instance/init.ts +++ b/src/core/instance/init.ts @@ -58,7 +58,7 @@ export function initMixin(Vue: typeof Component) { initLifecycle(vm) initEvents(vm) initRender(vm) - callHook(vm, 'beforeCreate') + callHook(vm, 'beforeCreate', undefined, false /* setContext */) initInjections(vm) // resolve injections before data/props initState(vm) initProvide(vm) // resolve provide after data/props diff --git a/src/core/instance/lifecycle.ts b/src/core/instance/lifecycle.ts index 171c4e5d9..0d1f07320 100644 --- a/src/core/instance/lifecycle.ts +++ b/src/core/instance/lifecycle.ts @@ -375,11 +375,16 @@ export function deactivateChildComponent(vm: Component, direct?: boolean) { } } -export function callHook(vm: Component, hook: string, args?: any[]) { +export function callHook( + vm: Component, + hook: string, + args?: any[], + setContext = true +) { // #7573 disable dep collection when invoking lifecycle hooks pushTarget() const prev = currentInstance - setCurrentInstance(vm) + setContext && setCurrentInstance(vm) const handlers = vm.$options[hook] const info = `${hook} hook` if (handlers) { @@ -390,6 +395,6 @@ export function callHook(vm: Component, hook: string, args?: any[]) { if (vm._hasHookEvent) { vm.$emit('hook:' + hook) } - setCurrentInstance(prev) + setContext && setCurrentInstance(prev) popTarget() }