From be6e050b89c8bcb904a30a0a5f435430d118b985 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 10 Oct 2016 23:03:21 -0400 Subject: [PATCH] fix `mounted` not called for manually mounted instance with parent (fix #3898) --- src/core/instance/lifecycle.js | 6 +++--- test/unit/features/options/lifecycle.spec.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/core/instance/lifecycle.js b/src/core/instance/lifecycle.js index fcd3f75a..f7700892 100644 --- a/src/core/instance/lifecycle.js +++ b/src/core/instance/lifecycle.js @@ -64,9 +64,9 @@ export function lifecycleMixin (Vue: Class) { vm._update(vm._render(), hydrating) }, noop) hydrating = false - // root instance, call mounted on self - // mounted is called for child components in its inserted hook - if (vm.$root === vm) { + // manually mounted instance, call mounted on self + // mounted is called for render-created child components in its inserted hook + if (vm.$vnode == null) { vm._isMounted = true callHook(vm, 'mounted') } diff --git a/test/unit/features/options/lifecycle.spec.js b/test/unit/features/options/lifecycle.spec.js index 87a623a6..b6ffe541 100644 --- a/test/unit/features/options/lifecycle.spec.js +++ b/test/unit/features/options/lifecycle.spec.js @@ -75,6 +75,20 @@ describe('Options lifecyce hooks', () => { expect(spy).toHaveBeenCalled() }) + // #3898 + it('should call for manually mounted instance with parent', () => { + const parent = new Vue() + expect(spy).not.toHaveBeenCalled() + new Vue({ + parent, + template: '
', + mounted () { + spy() + } + }).$mount() + expect(spy).toHaveBeenCalled() + }) + it('should mount child parent in correct order', () => { const calls = [] new Vue({