fix(compiler-core): make v-once work with v-if/else-if/else (#2182)

Partial fix for #2035
This commit is contained in:
HcySunYang
2020-10-05 23:58:37 +08:00
committed by GitHub
parent 752ecee407
commit 9499871582
3 changed files with 35 additions and 13 deletions

View File

@@ -99,15 +99,26 @@ describe('compiler: v-once transform', () => {
expect(generate(root).code).toMatchSnapshot()
})
test('with v-if', () => {
const root = transformWithOnce(`<div v-if="true" v-once />`)
test('with v-if/else', () => {
const root = transformWithOnce(`<div v-if="BOOLEAN" v-once /><p v-else/>`)
expect(root.cached).toBe(1)
expect(root.helpers).toContain(SET_BLOCK_TRACKING)
expect(root.children[0]).toMatchObject({
type: NodeTypes.IF,
// should cache the entire v-if expression, not just a single branch
// should cache the entire v-if/else-if/else expression, not just a single branch
codegenNode: {
type: NodeTypes.JS_CACHE_EXPRESSION
type: NodeTypes.JS_CACHE_EXPRESSION,
value: {
type: NodeTypes.JS_CONDITIONAL_EXPRESSION,
consequent: {
type: NodeTypes.VNODE_CALL,
tag: `"div"`
},
alternate: {
type: NodeTypes.VNODE_CALL,
tag: `"p"`
}
}
}
})
})