context: CodegenContext,
): CodeFragment[] {
const { seenChildIndexes } = context
- let { parent, childIndex, anchor } = operation
+ const { parent, childIndex, anchor } = operation
const insertionAnchor =
anchor == null
? undefined
: `n${anchor}`
// the index of next block node, used to locate node during hydration
+ // only passed when anchor is null and childIndex > 0
let index: number | undefined
- if (anchor == null) {
+ if (anchor == null && childIndex) {
const existingOffset = seenChildIndexes.get(parent!)
- index = existingOffset ? existingOffset + 1 : childIndex
- if (index) seenChildIndexes.set(parent!, index)
+ seenChildIndexes.set(
+ parent!,
+ (index = existingOffset ? existingOffset + 1 : childIndex),
+ )
}
return [
const { children } = dynamic
let offset = 0
+ let ifBranchCount = 0
let prev: [variable: string, elementIndex: number] | undefined
for (const [index, child] of children.entries()) {
+ if (child.isIfBranch) ifBranchCount++
if (child.flags & DynamicFlag.NON_TEMPLATE) {
offset--
}
// this ensures that insertionAnchor points to the current node itself
// rather than its next sibling, since insertionAnchor is used as the
// hydration node
- `${asAnchor ? index - 1 : index}`
+ `${
+ (asAnchor ? index - 1 : index) -
+ // treat v-if/v-else/v-else-if as a single node
+ ifBranchCount
+ }`
if (elementIndex === 0) {
pushBlock(...genCall(helper('child'), from, childIndex))
hasDynamicChild?: boolean
operation?: OperationNode
needsKey?: boolean
+ isIfBranch?: boolean
}
export interface IREffect {
prevDynamics,
context,
-1 /* prepend */,
- children.findIndex(c => c === prevDynamics[0]),
+ getChildIndex(children, prevDynamics[0]),
)
}
prevDynamics = []
prevDynamics,
context,
undefined,
- children.findIndex(c => c === prevDynamics[0]),
+ getChildIndex(children, prevDynamics[0]),
)
}
}
}
}
}
+
+function getChildIndex(
+ children: IRDynamicInfo[],
+ child: IRDynamicInfo,
+): number {
+ let index = 0
+ for (const c of children) {
+ // treat v-if/v-else/v-else-if as a single node
+ if (c.isIfBranch) continue
+ if (c === child) break
+ index++
+ }
+ return index
+}
}
}
} else {
+ context.dynamic.isIfBranch = true
// check the adjacent v-if
const siblingIf = getSiblingIf(context, true)