From bd89ce53a9de417a9372630bb5d433a40acc1a53 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Aug 2022 15:37:13 +0800 Subject: [PATCH] fix: ensure render watcher of manually created instance is correctly tracked in owner scope fix #12701 --- src/core/instance/lifecycle.ts | 2 ++ src/core/observer/watcher.ts | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/instance/lifecycle.ts b/src/core/instance/lifecycle.ts index df70b711..fec330ad 100644 --- a/src/core/instance/lifecycle.ts +++ b/src/core/instance/lifecycle.ts @@ -209,6 +209,7 @@ export function mountComponent( // we set this to vm._watcher inside the watcher's constructor // since the watcher's initial patch may call $forceUpdate (e.g. inside child // component's mounted hook), which relies on vm._watcher being already defined + vm._scope.on() new Watcher( vm, updateComponent, @@ -216,6 +217,7 @@ export function mountComponent( watcherOptions, true /* isRenderWatcher */ ) + vm._scope.off() hydrating = false // flush buffer for flush: "pre" watchers queued in setup() diff --git a/src/core/observer/watcher.ts b/src/core/observer/watcher.ts index b1f491ac..00bf19b9 100644 --- a/src/core/observer/watcher.ts +++ b/src/core/observer/watcher.ts @@ -72,10 +72,8 @@ export default class Watcher implements DepTarget { isRenderWatcher?: boolean ) { recordEffectScope(this, activeEffectScope || (vm ? vm._scope : undefined)) - if ((this.vm = vm)) { - if (isRenderWatcher) { - vm._watcher = this - } + if ((this.vm = vm) && isRenderWatcher) { + vm._watcher = this } // options if (options) {