Skip to content

Commit

Permalink
fix: do not set currentInstance in beforeCreate
Browse files Browse the repository at this point in the history
fix #12636
  • Loading branch information
yyx990803 committed Jul 12, 2022
1 parent 04b4703 commit 0825d30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/instance/init.ts
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/core/instance/lifecycle.ts
Expand Up @@ -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) {
Expand All @@ -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()
}

0 comments on commit 0825d30

Please sign in to comment.