Skip to content

Commit 4cca507

Browse files
committed
fix v-for on v-else branch regression (fix vuejs#4464)
1 parent 6918436 commit 4cca507

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/compiler/codegen/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,18 @@ function genChildren (el: ASTElement, checkSkip?: boolean): string | void {
298298
function canSkipNormalization (children): boolean {
299299
for (let i = 0; i < children.length; i++) {
300300
const el: any = children[i]
301-
if (el.for || el.tag === 'template' || el.tag === 'slot' ||
302-
(el.if && el.ifConditions.some(c => c.tag === 'template'))) {
301+
if (needsNormalization(el) ||
302+
(el.if && el.ifConditions.some(c => needsNormalization(c.block)))) {
303303
return false
304304
}
305305
}
306306
return true
307307
}
308308

309+
function needsNormalization (el) {
310+
return el.for || el.tag === 'template' || el.tag === 'slot'
311+
}
312+
309313
function genNode (node: ASTNode) {
310314
if (node.type === 1) {
311315
return genElement(node)

test/unit/features/directives/if.spec.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Directive v-if', () => {
141141
const vm = new Vue({
142142
template: `
143143
<div>
144-
<span v-for="item,i in list" v-if="item.value">{{i}}</span>
144+
<span v-for="(item, i) in list" v-if="item.value">{{i}}</span>
145145
</div>
146146
`,
147147
data: {
@@ -169,7 +169,7 @@ describe('Directive v-if', () => {
169169
const vm = new Vue({
170170
template: `
171171
<div>
172-
<span v-for="item,i in list" v-if="item.value">hello</span>
172+
<span v-for="(item, i) in list" v-if="item.value">hello</span>
173173
<span v-else>bye</span>
174174
</div>
175175
`,
@@ -194,6 +194,25 @@ describe('Directive v-if', () => {
194194
}).then(done)
195195
})
196196

197+
it('should work with v-for on v-else branch', done => {
198+
const vm = new Vue({
199+
template: `
200+
<div>
201+
<span v-if="false">hello</span>
202+
<span v-else v-for="item in list">{{ item }}</span>
203+
</div>
204+
`,
205+
data: {
206+
list: [1, 2, 3]
207+
}
208+
}).$mount()
209+
expect(vm.$el.textContent.trim()).toBe('123')
210+
vm.list.reverse()
211+
waitForUpdate(() => {
212+
expect(vm.$el.textContent.trim()).toBe('321')
213+
}).then(done)
214+
})
215+
197216
it('should work properly on component root', done => {
198217
const vm = new Vue({
199218
template: `

0 commit comments

Comments
 (0)