fix: remove wrong observe toggle, properly disable tracking in setup()

This commit is contained in:
Evan You 2022-06-20 14:32:50 +08:00
parent 9d54f8bdfe
commit 2d67641656
2 changed files with 30 additions and 3 deletions

View File

@ -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)) {

View File

@ -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)
})
})