Skip to content

Commit

Permalink
fix(watch): fix pre watchers not flushed on mount for nested component
Browse files Browse the repository at this point in the history
fix #12569
  • Loading branch information
yyx990803 committed Jun 22, 2022
1 parent fb7f5f0 commit 7a3aa3f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/core/instance/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,17 @@ export function mountComponent(
)
hydrating = false

// flush buffer for flush: "pre" watchers queued in setup()
const preWatchers = vm._preWatchers
if (preWatchers) {
for (let i = 0; i < preWatchers.length; i++) {
preWatchers[i].run()
}
}

// manually mounted instance, call mounted on self
// mounted is called for render-created child components in its inserted hook
if (vm.$vnode == null) {
const preWatchers = vm._preWatchers
if (preWatchers) {
for (let i = 0; i < preWatchers.length; i++) {
preWatchers[i].run()
}
}
vm._isMounted = true
callHook(vm, 'mounted')
}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/features/v3/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ describe('api: watch', () => {
expect(result2).toBe(true)
})

// #12569
it('flush:pre watcher triggered before component mount (in child components)', () => {
const count = ref(0)
const spy = vi.fn()
const Comp = {
setup() {
watch(count, spy)
count.value++
return h => h('div')
}
}
new Vue({
render: h => h(Comp)
}).$mount()
expect(spy).toHaveBeenCalledTimes(1)
})

it('flush timing: post', async () => {
const count = ref(0)
let result
Expand Down

0 comments on commit 7a3aa3f

Please sign in to comment.