Skip to content

Commit 0825d30

Browse files
committed
fix: do not set currentInstance in beforeCreate
fix #12636
1 parent 04b4703 commit 0825d30

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/core/instance/init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function initMixin(Vue: typeof Component) {
5858
initLifecycle(vm)
5959
initEvents(vm)
6060
initRender(vm)
61-
callHook(vm, 'beforeCreate')
61+
callHook(vm, 'beforeCreate', undefined, false /* setContext */)
6262
initInjections(vm) // resolve injections before data/props
6363
initState(vm)
6464
initProvide(vm) // resolve provide after data/props

src/core/instance/lifecycle.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,16 @@ export function deactivateChildComponent(vm: Component, direct?: boolean) {
375375
}
376376
}
377377

378-
export function callHook(vm: Component, hook: string, args?: any[]) {
378+
export function callHook(
379+
vm: Component,
380+
hook: string,
381+
args?: any[],
382+
setContext = true
383+
) {
379384
// #7573 disable dep collection when invoking lifecycle hooks
380385
pushTarget()
381386
const prev = currentInstance
382-
setCurrentInstance(vm)
387+
setContext && setCurrentInstance(vm)
383388
const handlers = vm.$options[hook]
384389
const info = `${hook} hook`
385390
if (handlers) {
@@ -390,6 +395,6 @@ export function callHook(vm: Component, hook: string, args?: any[]) {
390395
if (vm._hasHookEvent) {
391396
vm.$emit('hook:' + hook)
392397
}
393-
setCurrentInstance(prev)
398+
setContext && setCurrentInstance(prev)
394399
popTarget()
395400
}

0 commit comments

Comments
 (0)