Skip to content

Commit

Permalink
fix(watch): template ref watcher should fire after owner instance mou…
Browse files Browse the repository at this point in the history
…nted hook

fix #12578
  • Loading branch information
yyx990803 committed Jun 26, 2022
1 parent 44ab1cd commit 089b27c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/v3/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,12 @@ function doWatch(
} else {
// pre
watcher.update = () => {
if (!instance || instance._isMounted) {
queueWatcher(watcher)
} else {
if (instance && instance === currentInstance) {
// pre-watcher triggered inside setup()
const buffer = instance._preWatchers || (instance._preWatchers = [])
if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
} else {
queueWatcher(watcher)
}
}
}
Expand Down
27 changes: 25 additions & 2 deletions test/unit/features/v3/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,8 @@ describe('api: watch', () => {
expect(calls).toMatchObject(['watch 3', 'mounted'])
})

// TODO
// vuejs/core#1852
it.skip('flush: post watcher should fire after template refs updated', async () => {
it('flush: post watcher should fire after template refs updated', async () => {
const toggle = ref(false)
let dom: HTMLElement | null = null

Expand Down Expand Up @@ -1093,4 +1092,28 @@ describe('api: watch', () => {
// own update effect
expect(instance!._scope.effects.length).toBe(1)
})

// #12578
test('template ref triggered watcher should fire after component mount', async () => {
const order: string[] = []
const Child = { template: '<div/>' }
const App = {
setup() {
const child = ref<any>(null)
onMounted(() => {
order.push('mounted')
})
watch(child, () => {
order.push('watcher')
})
return { child }
},
components: { Child },
template: `<Child ref="child"/>`
}
new Vue(App).$mount()

await nextTick()
expect(order).toMatchObject([`mounted`, `watcher`])
})
})

0 comments on commit 089b27c

Please sign in to comment.