From 83399976b16441ebd6afc32d164988756e89aaa4 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 10 Aug 2022 15:49:01 +0800 Subject: [PATCH] fix(mp): dynamic scoped slot with v-for --- .../uni-mp-compiler/__tests__/slot.spec.ts | 7 +++++++ .../src/transforms/transformSlot.ts | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/uni-mp-compiler/__tests__/slot.spec.ts b/packages/uni-mp-compiler/__tests__/slot.spec.ts index 241be5429..66825f22a 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 462788d02..1fdeb2c86 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(`+'-'+`) } } -- GitLab