fix(compiler-core): generate incremental keys for v-if/else-if/else chains (#1589)
fix #1587
This commit is contained in:
@@ -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
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user