From 2d676416566c06d7c789899c92bf0f6924ec284a Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 20 Jun 2022 14:32:50 +0800 Subject: [PATCH] fix: remove wrong observe toggle, properly disable tracking in setup() --- src/v3/apiSetup.ts | 6 +++--- test/unit/features/v3/apiSetup.spec.ts | 27 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/v3/apiSetup.ts b/src/v3/apiSetup.ts index dcb263cb7f2..b3540fd75b5 100644 --- a/src/v3/apiSetup.ts +++ b/src/v3/apiSetup.ts @@ -1,6 +1,6 @@ import { Component } from 'types/component' import { PropOptions } from 'types/options' -import { toggleObserving } from '../core/observer' +import { popTarget, pushTarget } from '../core/observer/dep' import { def, invokeWithErrorHandling, isReserved, warn } from '../core/util' import VNode from '../core/vdom/vnode' import { @@ -31,7 +31,7 @@ export function initSetup(vm: Component) { const ctx = (vm._setupContext = createSetupContext(vm)) setCurrentInstance(vm) - toggleObserving(false) + pushTarget() const setupResult = invokeWithErrorHandling( setup, null, @@ -39,7 +39,7 @@ export function initSetup(vm: Component) { vm, `setup` ) - toggleObserving(true) + popTarget() setCurrentInstance() if (isFunction(setupResult)) { diff --git a/test/unit/features/v3/apiSetup.spec.ts b/test/unit/features/v3/apiSetup.spec.ts index 1fd415fabff..118686366e8 100644 --- a/test/unit/features/v3/apiSetup.spec.ts +++ b/test/unit/features/v3/apiSetup.spec.ts @@ -267,4 +267,31 @@ describe('api: setup context', () => { } }).$mount() }) + + it('should not track dep accessed in setup', async () => { + const spy = vi.fn() + const msg = ref('hi') + + const Child = { + setup: () => { + msg.value + return () => {} + } + } + + new Vue({ + setup() { + return h => { + spy() + return h(Child) + } + } + }).$mount() + + expect(spy).toHaveBeenCalledTimes(1) + + msg.value = 'bye' + await nextTick() + expect(spy).toHaveBeenCalledTimes(1) + }) })