Skip to content

Commit 089b27c

Browse files
committed
fix(watch): template ref watcher should fire after owner instance mounted hook
fix #12578
1 parent 44ab1cd commit 089b27c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/v3/apiWatch.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,12 @@ function doWatch(
320320
} else {
321321
// pre
322322
watcher.update = () => {
323-
if (!instance || instance._isMounted) {
324-
queueWatcher(watcher)
325-
} else {
323+
if (instance && instance === currentInstance) {
324+
// pre-watcher triggered inside setup()
326325
const buffer = instance._preWatchers || (instance._preWatchers = [])
327326
if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
327+
} else {
328+
queueWatcher(watcher)
328329
}
329330
}
330331
}

test/unit/features/v3/apiWatch.spec.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,8 @@ describe('api: watch', () => {
628628
expect(calls).toMatchObject(['watch 3', 'mounted'])
629629
})
630630

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

@@ -1093,4 +1092,28 @@ describe('api: watch', () => {
10931092
// own update effect
10941093
expect(instance!._scope.effects.length).toBe(1)
10951094
})
1095+
1096+
// #12578
1097+
test('template ref triggered watcher should fire after component mount', async () => {
1098+
const order: string[] = []
1099+
const Child = { template: '<div/>' }
1100+
const App = {
1101+
setup() {
1102+
const child = ref<any>(null)
1103+
onMounted(() => {
1104+
order.push('mounted')
1105+
})
1106+
watch(child, () => {
1107+
order.push('watcher')
1108+
})
1109+
return { child }
1110+
},
1111+
components: { Child },
1112+
template: `<Child ref="child"/>`
1113+
}
1114+
new Vue(App).$mount()
1115+
1116+
await nextTick()
1117+
expect(order).toMatchObject([`mounted`, `watcher`])
1118+
})
10961119
})

0 commit comments

Comments
 (0)