Skip to content

Commit abfa6fb

Browse files
author
liximomo
committed
fix(compiler): normalize nested arrays when use functional components with v-for
vuejs#8468
1 parent 21112ec commit abfa6fb

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/compiler/codegen/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ export function genChildren (
389389
el.tag !== 'template' &&
390390
el.tag !== 'slot'
391391
) {
392-
return (altGenElement || genElement)(el, state)
392+
// because el may be a functional component and return an Array instead of a single root.
393+
// In this case, just a simple normalization is needed
394+
return `${(altGenElement || genElement)(el, state)},1`
393395
}
394396
const normalizationType = checkSkip
395397
? getNormalizationType(children, state.maybeComponent)

test/unit/features/options/functional.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,25 @@ describe('Options functional', () => {
316316
triggerEvent(parent.$el.querySelector('.clickable'), 'click')
317317
waitForUpdate(assertMarkup).then(done)
318318
})
319+
320+
// #8468
321+
it('should normalize nested arrays when use functional components with v-for', () => {
322+
const Foo = {
323+
functional: true,
324+
props: {
325+
name: {}
326+
},
327+
render (h, context) {
328+
return [h('span', 'hi'), h('span', context.props.name)]
329+
}
330+
}
331+
const vm = new Vue({
332+
template: `<div><foo v-for="name in names" :name="name" /></div>`,
333+
data: {
334+
names: ['foo', 'bar']
335+
},
336+
components: { Foo }
337+
}).$mount()
338+
expect(vm.$el.innerHTML).toBe('<span>hi</span><span>foo</span><span>hi</span><span>bar</span>')
339+
})
319340
})

test/unit/modules/compiler/codegen.spec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@ describe('codegen', () => {
5454
it('generate v-for directive', () => {
5555
assertCodegen(
5656
'<div><li v-for="item in items" :key="item.uid"></li></div>',
57-
`with(this){return _c('div',_l((items),function(item){return _c('li',{key:item.uid})}))}`
57+
`with(this){return _c('div',_l((items),function(item){return _c('li',{key:item.uid})}),1)}`
5858
)
5959
// iterator syntax
6060
assertCodegen(
6161
'<div><li v-for="(item, i) in items"></li></div>',
62-
`with(this){return _c('div',_l((items),function(item,i){return _c('li')}))}`
62+
`with(this){return _c('div',_l((items),function(item,i){return _c('li')}),1)}`
6363
)
6464
assertCodegen(
6565
'<div><li v-for="(item, key, index) in items"></li></div>',
66-
`with(this){return _c('div',_l((items),function(item,key,index){return _c('li')}))}`
66+
`with(this){return _c('div',_l((items),function(item,key,index){return _c('li')}),1)}`
6767
)
6868
// destructuring
6969
assertCodegen(
7070
'<div><li v-for="{ a, b } in items"></li></div>',
71-
`with(this){return _c('div',_l((items),function({ a, b }){return _c('li')}))}`
71+
`with(this){return _c('div',_l((items),function({ a, b }){return _c('li')}),1)}`
7272
)
7373
assertCodegen(
7474
'<div><li v-for="({ a, b }, key, index) in items"></li></div>',
75-
`with(this){return _c('div',_l((items),function({ a, b },key,index){return _c('li')}))}`
75+
`with(this){return _c('div',_l((items),function({ a, b },key,index){return _c('li')}),1)}`
7676
)
7777
// v-for with extra element
7878
assertCodegen(
@@ -126,7 +126,7 @@ describe('codegen', () => {
126126
it('generate ref on v-for', () => {
127127
assertCodegen(
128128
'<ul><li v-for="item in items" ref="component1"></li></ul>',
129-
`with(this){return _c('ul',_l((items),function(item){return _c('li',{ref:"component1",refInFor:true})}))}`
129+
`with(this){return _c('ul',_l((items),function(item){return _c('li',{ref:"component1",refInFor:true})}),1)}`
130130
)
131131
})
132132

@@ -550,7 +550,7 @@ describe('codegen', () => {
550550
it('generate static trees inside v-for', () => {
551551
assertCodegen(
552552
`<div><div v-for="i in 10"><p><span></span></p></div></div>`,
553-
`with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}))}`,
553+
`with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}),1)}`,
554554
[`with(this){return _c('p',[_c('span')])}`]
555555
)
556556
})

0 commit comments

Comments
 (0)