提交 83399976 编写于 作者: fxy060608's avatar fxy060608

fix(mp): dynamic scoped slot with v-for

上级 63aa08d4
...@@ -30,6 +30,13 @@ describe('compiler: transform slot', () => { ...@@ -30,6 +30,13 @@ describe('compiler: transform slot', () => {
`<button><slot name="{{a}}"></slot></button>`, `<button><slot name="{{a}}"></slot></button>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _d('title' + _ctx.index), b: _r(_d('title' + _ctx.index), { content: { name: 'name1' } }) } return { a: _d('title' + _ctx.index), b: _r(_d('title' + _ctx.index), { content: { name: 'name1' } }) }
}`
)
assert(
`<view v-for="(item,index) in 3" :key="index"><slot :name="'title'+index" :content="{name:'name'+index}"></slot></view>`,
`<view wx:for="{{a}}" wx:for-item="item" wx:key="c"><slot name="{{item.a}}"></slot></view>`,
`(_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 }; }) }
}` }`
) )
}) })
......
...@@ -53,12 +53,22 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) { ...@@ -53,12 +53,22 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) {
} }
if (p.name === 'bind' && isStaticArgOf(p.arg, 'name')) { if (p.name === 'bind' && isStaticArgOf(p.arg, 'name')) {
if (p.exp) { if (p.exp) {
const slotKey = parseScopedSlotKey(context)
// renderSlot 第三个参数已经传了 slotKey
slotName = createCompoundExpression([ slotName = createCompoundExpression([
context.helperString(DYNAMIC_SLOT) + '(', context.helperString(DYNAMIC_SLOT) + '(',
p.exp, p.exp,
')', ')',
]) ])
p.exp = rewriteExpression(slotName, context) p.exp = rewriteExpression(
createCompoundExpression([
context.helperString(DYNAMIC_SLOT) + '(',
p.exp,
slotKey ? `+'-'+` + slotKey : '',
')',
]),
context
)
} }
} else { } else {
if (p.name === 'bind' && p.arg && isStaticExp(p.arg)) { if (p.name === 'bind' && p.arg && isStaticExp(p.arg)) {
...@@ -166,16 +176,16 @@ function createScopedSlotDirectiveNode( ...@@ -166,16 +176,16 @@ function createScopedSlotDirectiveNode(
function parseScopedSlotKey(context: TransformContext) { function parseScopedSlotKey(context: TransformContext) {
let { currentScope } = context let { currentScope } = context
const indexs: string[] = [] const indexes: string[] = []
while (currentScope) { while (currentScope) {
if (isVForScope(currentScope)) { if (isVForScope(currentScope)) {
indexs.push(currentScope.indexAlias) indexes.push(currentScope.indexAlias)
} }
currentScope = currentScope.parent! currentScope = currentScope.parent!
} }
const inFor = !!indexs.length const inFor = !!indexes.length
if (inFor) { if (inFor) {
return indexs.reverse().join(`+'-'+`) return indexes.reverse().join(`+'-'+`)
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册