fix(compiler-core): generate incremental keys for v-if/else-if/else chains (#1589)

fix #1587
This commit is contained in:
HcySunYang
2020-07-15 21:21:40 +08:00
committed by GitHub
parent 46158b4591
commit 64c7b2f9ce
3 changed files with 123 additions and 5 deletions

View File

@@ -37,13 +37,26 @@ export const transformIf = createStructuralDirectiveTransform(
/^(if|else|else-if)$/,
(node, dir, context) => {
return processIf(node, dir, context, (ifNode, branch, isRoot) => {
// #1587: We need to dynamically increment the key based on the current
// node's sibling nodes, since chained v-if/else branches are
// rendered at the same depth
const siblings = context.parent!.children
let i = siblings.indexOf(ifNode)
let key = 0
while (i-- >= 0) {
const sibling = siblings[i]
if (sibling && sibling.type === NodeTypes.IF) {
key += sibling.branches.length
}
}
// Exit callback. Complete the codegenNode when all children have been
// transformed.
return () => {
if (isRoot) {
ifNode.codegenNode = createCodegenNodeForBranch(
branch,
0,
key,
context
) as IfConditionalExpression
} else {
@@ -57,7 +70,7 @@ export const transformIf = createStructuralDirectiveTransform(
}
parentCondition.alternate = createCodegenNodeForBranch(
branch,
ifNode.branches.length - 1,
key + ifNode.branches.length - 1,
context
)
}