Skip to content

Commit dd21eac

Browse files
committed
fix: fix async component resolving in sibling mounted hook
fix #7107
1 parent 604e081 commit dd21eac

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/core/instance/lifecycle.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ export function mountComponent (
193193
}
194194
}
195195

196-
vm._watcher = new Watcher(vm, updateComponent, noop)
196+
// we set this to vm._watcher inside the wathcer's constructor
197+
// since the watcher's initial patch may call $forceUpdate (e.g. inside child
198+
// component's mounted hook), which relies on vm._watcher being already defined
199+
new Watcher(vm, updateComponent, noop, null, true /* isRenderWatcher */)
197200
hydrating = false
198201

199202
// manually mounted instance, call mounted on self

src/core/observer/watcher.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ export default class Watcher {
4444
vm: Component,
4545
expOrFn: string | Function,
4646
cb: Function,
47-
options?: Object
47+
options?: ?Object,
48+
isRenderWatcher?: boolean
4849
) {
4950
this.vm = vm
51+
if (isRenderWatcher) {
52+
vm._watcher = this
53+
}
5054
vm._watchers.push(this)
5155
// options
5256
if (options) {

test/unit/features/component/component-async.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,34 @@ describe('Component async', () => {
342342
done()
343343
}, 50)
344344
})
345+
346+
// #7107
347+
it(`should work when resolving sync in sibling component's mounted hook`, done => {
348+
let resolveTwo
349+
350+
const vm = new Vue({
351+
template: `<div><one/> <two/></div>`,
352+
components: {
353+
one: {
354+
template: `<div>one</div>`,
355+
mounted () {
356+
resolveTwo()
357+
}
358+
},
359+
two: resolve => {
360+
resolveTwo = () => {
361+
resolve({
362+
template: `<div>two</div>`
363+
})
364+
}
365+
}
366+
}
367+
}).$mount()
368+
369+
expect(vm.$el.textContent).toBe('one ')
370+
waitForUpdate(() => {
371+
expect(vm.$el.textContent).toBe('one two')
372+
}).then(done)
373+
})
345374
})
346375
})

0 commit comments

Comments
 (0)