Browse Source

fix: fix single v-for child optimization

Evan You 7 years ago
parent
commit
847e493768

+ 3 - 2
src/core/instance/render-helpers/render-list.js

@@ -32,8 +32,9 @@ export function renderList (
       ret[i] = render(val[key], key, i)
     }
   }
-  if (isDef(ret)) {
-    (ret: any)._isVList = true
+  if (!isDef(ret)) {
+    ret = []
   }
+  (ret: any)._isVList = true
   return ret
 }

+ 0 - 3
src/core/vdom/helpers/normalize-children.js

@@ -16,9 +16,6 @@ import { isFalse, isTrue, isDef, isUndef, isPrimitive } from 'shared/util'
 // thing with Array.prototype.concat. It is guaranteed to be only 1-level deep
 // because functional components already normalize their own children.
 export function simpleNormalizeChildren (children: any) {
-  if (!Array.isArray(children)) {
-    return
-  }
   for (let i = 0; i < children.length; i++) {
     if (Array.isArray(children[i])) {
       return Array.prototype.concat.apply([], children)

+ 18 - 1
test/unit/features/directives/for.spec.js

@@ -489,7 +489,7 @@ describe('Directive v-for', () => {
     const vm = new Vue({
       template:
         '<div attr>' +
-          '<foo v-for="item in list">{{ item }}</foo>' +
+          '<foo v-for="item in list" :key="item">{{ item }}</foo>' +
         '</div>',
       data: {
         list: undefined
@@ -507,6 +507,23 @@ describe('Directive v-for', () => {
     }).then(done)
   })
 
+  it('elements with v-for and empty list', done => {
+    const vm = new Vue({
+      template:
+        '<div attr>' +
+          '<div v-for="item in list">{{ item }}</div>' +
+        '</div>',
+      data: {
+        list: undefined
+      }
+    }).$mount()
+    expect(vm.$el.innerHTML).toBe('')
+    vm.list = [1, 2, 3]
+    waitForUpdate(() => {
+      expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
+    }).then(done)
+  })
+
   const supportsDestructuring = (() => {
     try {
       new Function('var { foo } = bar')