mirror of
https://gitee.com/vuejs/vue.git
synced 2024-11-30 02:57:43 +08:00
fix(watch): template ref watcher should fire after owner instance mounted hook
fix #12578
This commit is contained in:
parent
44ab1cda74
commit
089b27c30b
@ -320,11 +320,12 @@ function doWatch(
|
|||||||
} else {
|
} else {
|
||||||
// pre
|
// pre
|
||||||
watcher.update = () => {
|
watcher.update = () => {
|
||||||
if (!instance || instance._isMounted) {
|
if (instance && instance === currentInstance) {
|
||||||
queueWatcher(watcher)
|
// pre-watcher triggered inside setup()
|
||||||
} else {
|
|
||||||
const buffer = instance._preWatchers || (instance._preWatchers = [])
|
const buffer = instance._preWatchers || (instance._preWatchers = [])
|
||||||
if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
|
if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
|
||||||
|
} else {
|
||||||
|
queueWatcher(watcher)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -628,9 +628,8 @@ describe('api: watch', () => {
|
|||||||
expect(calls).toMatchObject(['watch 3', 'mounted'])
|
expect(calls).toMatchObject(['watch 3', 'mounted'])
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO
|
|
||||||
// vuejs/core#1852
|
// 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)
|
const toggle = ref(false)
|
||||||
let dom: HTMLElement | null = null
|
let dom: HTMLElement | null = null
|
||||||
|
|
||||||
@ -1093,4 +1092,28 @@ describe('api: watch', () => {
|
|||||||
// own update effect
|
// own update effect
|
||||||
expect(instance!._scope.effects.length).toBe(1)
|
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`])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user