Skip to content

Commit

Permalink
fix(watch): fix watchers triggered in mounted hook
Browse files Browse the repository at this point in the history
fix #12624
  • Loading branch information
yyx990803 committed Jul 8, 2022
1 parent 012e10c commit 8904ca7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/v3/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ function doWatch(
} else {
// pre
watcher.update = () => {
if (instance && instance === currentInstance) {
// pre-watcher triggered inside setup()
if (instance && instance === currentInstance && !instance._isMounted) {
// pre-watcher triggered before
const buffer = instance._preWatchers || (instance._preWatchers = [])
if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/features/v3/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,4 +1116,24 @@ describe('api: watch', () => {
await nextTick()
expect(order).toMatchObject([`mounted`, `watcher`])
})

// #12624
test('pre watch triggered in mounted hook', async () => {
const spy = vi.fn()
new Vue({
setup() {
const c = ref(0)

onMounted(() => {
c.value++
})

watchEffect(() => spy(c.value))
return () => {}
}
}).$mount()
expect(spy).toHaveBeenCalledTimes(1)
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
})
})

0 comments on commit 8904ca7

Please sign in to comment.