Prechádzať zdrojové kódy

Fix when functional component render method retrun null (fix #5536) (#5539)

* fix:create empty vnode when functional component return null

* add test

* use isDef
laoxiong 9 rokov pred
rodič
commit
3b426efe60

+ 1 - 1
src/core/vdom/create-element.js

@@ -99,7 +99,7 @@ export function _createElement (
     // direct component options / constructor
     vnode = createComponent(tag, data, context, children)
   }
-  if (vnode !== undefined) {
+  if (isDef(vnode)) {
     if (ns) applyNS(vnode, ns)
     return vnode
   } else {

+ 18 - 0
test/unit/features/options/functional.spec.js

@@ -1,4 +1,5 @@
 import Vue from 'vue'
+import { createEmptyVNode } from 'core/vdom/vnode'
 
 describe('Options functional', () => {
   it('should work', done => {
@@ -167,4 +168,21 @@ describe('Options functional', () => {
       vm.$destroy()
     }).then(done)
   })
+
+  it('create empty vnode when render return null', () => {
+    const child = {
+      functional: true,
+      render () {
+        return null
+      }
+    }
+    const vm = new Vue({
+      components: {
+        child
+      }
+    })
+    const h = vm.$createElement
+    const vnode = h('child')
+    expect(vnode).toEqual(createEmptyVNode())
+  })
 })