diff --git a/packages/uni-mp-compiler/__tests__/slot.spec.ts b/packages/uni-mp-compiler/__tests__/slot.spec.ts
index 241be5429fa86a005ef6802e429e36bcfc4b1722..66825f22aeb17c088b8de2cae45c5b4de2b1c50d 100644
--- a/packages/uni-mp-compiler/__tests__/slot.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/slot.spec.ts
@@ -30,6 +30,13 @@ describe('compiler: transform slot', () => {
``,
`(_ctx, _cache) => {
return { a: _d('title' + _ctx.index), b: _r(_d('title' + _ctx.index), { content: { name: 'name1' } }) }
+}`
+ )
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return { a: _f(3, (item, index, i0) => { return { a: _d('title' + index + '-' + i0), b: _r(_d('title' + index), { content: { name: 'name' + index } }, i0), c: index }; }) }
}`
)
})
diff --git a/packages/uni-mp-compiler/src/transforms/transformSlot.ts b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
index 462788d022987526f8f9a2fb4e76496545c0b852..1fdeb2c86ce6d647798a6ce0c1a265e1a07a6721 100644
--- a/packages/uni-mp-compiler/src/transforms/transformSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
@@ -53,12 +53,22 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) {
}
if (p.name === 'bind' && isStaticArgOf(p.arg, 'name')) {
if (p.exp) {
+ const slotKey = parseScopedSlotKey(context)
+ // renderSlot 第三个参数已经传了 slotKey
slotName = createCompoundExpression([
context.helperString(DYNAMIC_SLOT) + '(',
p.exp,
')',
])
- p.exp = rewriteExpression(slotName, context)
+ p.exp = rewriteExpression(
+ createCompoundExpression([
+ context.helperString(DYNAMIC_SLOT) + '(',
+ p.exp,
+ slotKey ? `+'-'+` + slotKey : '',
+ ')',
+ ]),
+ context
+ )
}
} else {
if (p.name === 'bind' && p.arg && isStaticExp(p.arg)) {
@@ -166,16 +176,16 @@ function createScopedSlotDirectiveNode(
function parseScopedSlotKey(context: TransformContext) {
let { currentScope } = context
- const indexs: string[] = []
+ const indexes: string[] = []
while (currentScope) {
if (isVForScope(currentScope)) {
- indexs.push(currentScope.indexAlias)
+ indexes.push(currentScope.indexAlias)
}
currentScope = currentScope.parent!
}
- const inFor = !!indexs.length
+ const inFor = !!indexes.length
if (inFor) {
- return indexs.reverse().join(`+'-'+`)
+ return indexes.reverse().join(`+'-'+`)
}
}