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

fix(mp): named slots with only child

上级 bc0a4d43
......@@ -36,7 +36,7 @@ function assert(
}
assert(
`<button><slot :name="name"/></button>`,
`<custom><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
`<slot wx:for="{{a}}" wx:for-item="item"></slot>`,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, index, i0) => { return { a: _r(\"default\", { key: index }) }; }) }
......
......@@ -34,6 +34,16 @@ describe('compiler: transform v-slot', () => {
)
})
test(`names slots with single child`, () => {
assert(
`<uni-list-item class="item"><template v-slot:body><view class="item"></view></template></uni-list-item>`,
`<uni-list-item u-s="{{['body']}}" class="item" u-i="2a9ec0b0-0"><view class="item" slot="body"></view></uni-list-item>`,
`(_ctx, _cache) => {
return {}
}`
)
})
test('scoped slots', () => {
assert(
`<custom><template v-slot:default="slotProps"><view>{{ slotProps.item }}</view></template></custom>`,
......
......@@ -2,6 +2,7 @@ import { hyphenate } from '@vue/shared'
import { SLOT_DEFAULT_NAME, dynamicSlotName } from '@dcloudio/uni-shared'
import {
formatMiniProgramEvent,
isElementNode,
MiniProgramCompilerOptions,
} from '@dcloudio/uni-cli-shared'
import {
......@@ -186,6 +187,21 @@ function genTemplate(node: TemplateNode, context: TemplateCodegenContext) {
}
// @ts-ignore
node.tagType = ElementTypes.ELEMENT
// 仅单个子节点的命名插槽(非作用域),直接使用子节点作为插槽使用,避免多增加的 view 节点影响 flex 排版
if (
slotProp &&
node.tag === 'view' &&
!isForElementNode(node) &&
node.children.length === 1
) {
const child = node.children[0]
if (isElementNode(child) && !isForElementNode(child)) {
child.props.push(slotProp)
return genElement(child, context)
}
}
return genElement(node, context)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册