Преглед на файлове

fix ref on empty component root (fix #3455)

Evan You преди 9 години
родител
ревизия
ce798c6f68
променени са 3 файла, в които са добавени 18 реда и са изтрити 1 реда
  1. 1 1
      src/core/vdom/modules/ref.js
  2. 5 0
      src/core/vdom/patch.js
  3. 12 0
      test/unit/features/ref.spec.js

+ 1 - 1
src/core/vdom/modules/ref.js

@@ -17,7 +17,7 @@ export default {
   }
 }
 
-function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) {
+export function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) {
   const key = vnode.data.ref
   if (!key) return
 

+ 5 - 0
src/core/vdom/patch.js

@@ -11,6 +11,7 @@ import config from '../config'
 import VNode from './vnode'
 import { isPrimitive, _toString, warn } from '../util/index'
 import { activeInstance } from '../instance/lifecycle'
+import { registerRef } from './modules/ref'
 
 const emptyData = {}
 const emptyNode = new VNode('', emptyData, [])
@@ -156,6 +157,10 @@ export function createPatchFunction (backend) {
       invokeCreateHooks(vnode, insertedVnodeQueue)
       setScope(vnode)
     } else {
+      // empty component root.
+      // skip all element-related modules except for ref (#3455)
+      registerRef(vnode)
+      // make sure to invoke the insert hook
       insertedVnodeQueue.push(vnode)
     }
   }

+ 12 - 0
test/unit/features/ref.spec.js

@@ -145,4 +145,16 @@ describe('ref', () => {
       expect(vm.$refs.list.every((comp, i) => comp.$el.textContent === String(i + 1))).toBe(true)
     }
   })
+
+  it('should register on component with empty roots', () => {
+    const vm = new Vue({
+      template: '<child ref="test"></child>',
+      components: {
+        child: {
+          template: '<div v-if="false"></div>'
+        }
+      }
+    }).$mount()
+    expect(vm.$refs.test).toBe(vm.$children[0])
+  })
 })