fix(watch): avoid pre watcher firing on unmount

fix #12703
This commit is contained in:
Evan You 2022-08-15 19:06:38 +08:00
parent bd89ce53a9
commit f0057b101e
2 changed files with 3 additions and 6 deletions

View File

@ -274,10 +274,7 @@ function doWatch(
let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
// overwrite default run // overwrite default run
watcher.run = () => { watcher.run = () => {
if ( if (!watcher.active) {
!watcher.active &&
!(flush === 'pre' && instance && instance._isBeingDestroyed)
) {
return return
} }
if (cb) { if (cb) {

View File

@ -542,7 +542,7 @@ describe('api: watch', () => {
expect(cb).not.toHaveBeenCalled() expect(cb).not.toHaveBeenCalled()
}) })
it('should fire on component unmount w/ flush: pre', async () => { it('should not fire on component unmount w/ flush: pre', async () => {
const toggle = ref(true) const toggle = ref(true)
const cb = vi.fn() const cb = vi.fn()
const Comp = { const Comp = {
@ -560,7 +560,7 @@ describe('api: watch', () => {
expect(cb).not.toHaveBeenCalled() expect(cb).not.toHaveBeenCalled()
toggle.value = false toggle.value = false
await nextTick() await nextTick()
expect(cb).toHaveBeenCalledTimes(1) expect(cb).not.toHaveBeenCalled()
}) })
// vuejs/core#1763 // vuejs/core#1763