fix(watch): fix pre watchers not flushed on mount for nested component

fix #12569
This commit is contained in:
Evan You 2022-06-22 10:20:02 +08:00
parent fb7f5f0b67
commit 7a3aa3faac
2 changed files with 25 additions and 6 deletions

View File

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

View File

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