Skip to content

Commit c1b84f8

Browse files
defccyyx990803
authored andcommitted
fix markStaticRoots with v-else (vuejs#4256)
1 parent 9fa4bbb commit c1b84f8

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/compiler/optimizer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@ function markStaticRoots (node: ASTNode, isInFor: boolean) {
7777
}
7878
if (node.children) {
7979
for (let i = 0, l = node.children.length; i < l; i++) {
80-
const child = node.children[i]
81-
isInFor = isInFor || !!node.for
82-
markStaticRoots(child, isInFor)
83-
if (child.type === 1 && child.elseBlock) {
84-
markStaticRoots(child.elseBlock, isInFor)
85-
}
80+
markStaticRoots(node.children[i], isInFor || !!node.for)
8681
}
8782
}
83+
if (node.elseBlock) {
84+
markStaticRoots(node.elseBlock, isInFor)
85+
}
8886
}
8987
}
9088

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ describe('Directive v-once', () => {
213213
}).then(done)
214214
})
215215

216+
it('should work inside v-for with nested v-else', done => {
217+
const vm = new Vue({
218+
data: {
219+
list: [{ id: 0, text: 'a', tester: true, truthy: 'y' }]
220+
},
221+
template: `
222+
<div v-if="0"></div>
223+
<div v-else>
224+
<div v-for="i in list" :key="i.id">
225+
<span v-if="i.tester" v-once>{{ i.truthy }}</span>
226+
<span v-else v-once>{{ i.text }}</span>
227+
</div>
228+
</div>
229+
`
230+
}).$mount()
231+
232+
expectTextContent(vm, 'y')
233+
vm.list[0].truthy = 'yy'
234+
waitForUpdate(() => {
235+
expectTextContent(vm, 'y')
236+
vm.list[0].tester = false
237+
}).then(() => {
238+
expectTextContent(vm, 'a')
239+
vm.list[0].text = 'nn'
240+
}).then(() => {
241+
expectTextContent(vm, 'a')
242+
}).then(done)
243+
})
244+
216245
it('should warn inside non-keyed v-for', () => {
217246
const vm = new Vue({
218247
data: {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,18 @@ describe('optimizer', () => {
209209
expect(ast.children[0].children[0].staticRoot).toBe(true)
210210
expect(ast.children[0].children[0].staticInFor).toBe(true)
211211
})
212+
213+
it('mark static trees inside v-for with nested v-else and v-once', () => {
214+
const ast = parse(`
215+
<div v-if="1"></div>
216+
<div v-else>
217+
<div v-for="i in 10" :key="i">
218+
<div v-if="1">{{ i }}</div>
219+
<div v-else v-once>{{ i }}</div>
220+
</div>
221+
<div>`, baseOptions)
222+
optimize(ast, baseOptions)
223+
expect(ast.elseBlock.children[0].children[0].elseBlock.staticRoot).toBe(false)
224+
expect(ast.elseBlock.children[0].children[0].elseBlock.staticInFor).toBe(true)
225+
})
212226
})

0 commit comments

Comments
 (0)