Browse Source

fix `mounted` not called for manually mounted instance with parent (fix #3898)

Evan You 9 years ago
parent
commit
be6e050b89
2 changed files with 17 additions and 3 deletions
  1. 3 3
      src/core/instance/lifecycle.js
  2. 14 0
      test/unit/features/options/lifecycle.spec.js

+ 3 - 3
src/core/instance/lifecycle.js

@@ -64,9 +64,9 @@ export function lifecycleMixin (Vue: Class<Component>) {
       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')
     }

+ 14 - 0
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: '<div></div>',
+        mounted () {
+          spy()
+        }
+      }).$mount()
+      expect(spy).toHaveBeenCalled()
+    })
+
     it('should mount child parent in correct order', () => {
       const calls = []
       new Vue({