Skip to content

Commit 4748760

Browse files
committed
fix: fix v-for component with undefined value
fix #9181
1 parent 984393f commit 4748760

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: src/core/vdom/helpers/normalize-children.js

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

Diff for: test/unit/features/directives/for.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,29 @@ describe('Directive v-for', () => {
484484
expect(vm.$el.textContent).toBe('12')
485485
})
486486

487+
// #9181
488+
it('components with v-for and empty list', done => {
489+
const vm = new Vue({
490+
template:
491+
'<div attr>' +
492+
'<foo v-for="item in list">{{ item }}</foo>' +
493+
'</div>',
494+
data: {
495+
list: undefined
496+
},
497+
components: {
498+
foo: {
499+
template: '<div><slot></slot></div>'
500+
},
501+
}
502+
}).$mount()
503+
expect(vm.$el.innerHTML).toBe('')
504+
vm.list = [1, 2, 3]
505+
waitForUpdate(() => {
506+
expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
507+
}).then(done)
508+
})
509+
487510
const supportsDestructuring = (() => {
488511
try {
489512
new Function('var { foo } = bar')

0 commit comments

Comments
 (0)