mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-02 03:57:36 +08:00
fix(watch): fix pre watchers not flushed on mount for nested component
fix #12569
This commit is contained in:
parent
fb7f5f0b67
commit
7a3aa3faac
@ -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')
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user