Skip to content

Commit 5110cae

Browse files
committed
fix(x-mp): 处理根节点包含 slot 标签的情况
1 parent b70b2e0 commit 5110cae

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/uni-mp-compiler/__tests__/root.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,53 @@ describe('compiler: root', () => {
157157
`(_ctx, _cache) => { "raw js"
158158
const __returned__ = _e({ a: _ctx.show }, _ctx.show ? _e({ b: _ctx.show2 }, _ctx.show2 ? { c: \`\${_ctx.u_s_b_h}px\` } : {}, { d: \`\${_ctx.u_s_b_h}px\` }) : {})
159159
return __returned__
160+
}`,
161+
{ isX: true }
162+
)
163+
})
164+
test('slot', () => {
165+
assert(
166+
`<slot>test</slot>`,
167+
`<block wx:if="{{$slots.d}}"><slot></slot></block><block wx:else>test</block>`,
168+
`(_ctx, _cache) => { "raw js"
169+
const __returned__ = {}
170+
return __returned__
171+
}`,
172+
{ isX: true }
173+
)
174+
assert(
175+
`<slot><view>test</view></slot>`,
176+
`<block wx:if="{{$slots.d}}"><slot></slot></block><block wx:else><view style="{{'--status-bar-height:' + a}}">test</view></block>`,
177+
`(_ctx, _cache) => { "raw js"
178+
const __returned__ = { a: \`\${_ctx.u_s_b_h}px\` }
179+
return __returned__
180+
}`,
181+
{ isX: true }
182+
)
183+
assert(
184+
`<slot>test1<view>test2</view></slot>`,
185+
`<block wx:if="{{$slots.d}}"><slot></slot></block><block wx:else>test1<view style="{{'--status-bar-height:' + a}}">test2</view></block>`,
186+
`(_ctx, _cache) => { "raw js"
187+
const __returned__ = { a: \`\${_ctx.u_s_b_h}px\` }
188+
return __returned__
189+
}`,
190+
{ isX: true }
191+
)
192+
assert(
193+
`<slot>test1</slot><view>test2</view>`,
194+
`<block wx:if="{{$slots.d}}"><slot></slot></block><block wx:else>test1</block><view style="{{'--status-bar-height:' + a}}">test2</view>`,
195+
`(_ctx, _cache) => { "raw js"
196+
const __returned__ = { a: \`\${_ctx.u_s_b_h}px\` }
197+
return __returned__
198+
}`,
199+
{ isX: true }
200+
)
201+
assert(
202+
`<slot name="test">test1</slot>`,
203+
`<block wx:if="{{$slots.test}}"><slot name="test"></slot></block><block wx:else>test1</block>`,
204+
`(_ctx, _cache) => { "raw js"
205+
const __returned__ = {}
206+
return __returned__
160207
}`,
161208
{ isX: true }
162209
)

packages/uni-mp-compiler/src/transforms/transformRoot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ function traverseChildren(node: ElementNode, context: TransformContext) {
9898
if (isUserComponent(node, context)) {
9999
return
100100
}
101-
if (node.tag !== 'template') {
102-
addStatusBarStyle(node, context)
101+
if (node.tag !== 'template' && node.tag !== 'slot') {
102+
isElementNode(node) && addStatusBarStyle(node, context)
103103
} else {
104104
node.children.forEach((child) => {
105105
traverseChildren(child as ElementNode, context)

0 commit comments

Comments
 (0)