File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments