fix v-for on v-else branch regression (fix #4464)

This commit is contained in:
Evan You 2016-12-13 12:02:37 -05:00
parent 6918436bf8
commit 4cca50725a
2 changed files with 27 additions and 4 deletions

View File

@ -298,14 +298,18 @@ function genChildren (el: ASTElement, checkSkip?: boolean): string | void {
function canSkipNormalization (children): boolean { function canSkipNormalization (children): boolean {
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const el: any = children[i] const el: any = children[i]
if (el.for || el.tag === 'template' || el.tag === 'slot' || if (needsNormalization(el) ||
(el.if && el.ifConditions.some(c => c.tag === 'template'))) { (el.if && el.ifConditions.some(c => needsNormalization(c.block)))) {
return false return false
} }
} }
return true return true
} }
function needsNormalization (el) {
return el.for || el.tag === 'template' || el.tag === 'slot'
}
function genNode (node: ASTNode) { function genNode (node: ASTNode) {
if (node.type === 1) { if (node.type === 1) {
return genElement(node) return genElement(node)

View File

@ -141,7 +141,7 @@ describe('Directive v-if', () => {
const vm = new Vue({ const vm = new Vue({
template: ` template: `
<div> <div>
<span v-for="item,i in list" v-if="item.value">{{i}}</span> <span v-for="(item, i) in list" v-if="item.value">{{i}}</span>
</div> </div>
`, `,
data: { data: {
@ -169,7 +169,7 @@ describe('Directive v-if', () => {
const vm = new Vue({ const vm = new Vue({
template: ` template: `
<div> <div>
<span v-for="item,i in list" v-if="item.value">hello</span> <span v-for="(item, i) in list" v-if="item.value">hello</span>
<span v-else>bye</span> <span v-else>bye</span>
</div> </div>
`, `,
@ -194,6 +194,25 @@ describe('Directive v-if', () => {
}).then(done) }).then(done)
}) })
it('should work with v-for on v-else branch', done => {
const vm = new Vue({
template: `
<div>
<span v-if="false">hello</span>
<span v-else v-for="item in list">{{ item }}</span>
</div>
`,
data: {
list: [1, 2, 3]
}
}).$mount()
expect(vm.$el.textContent.trim()).toBe('123')
vm.list.reverse()
waitForUpdate(() => {
expect(vm.$el.textContent.trim()).toBe('321')
}).then(done)
})
it('should work properly on component root', done => { it('should work properly on component root', done => {
const vm = new Vue({ const vm = new Vue({
template: ` template: `