diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js
index 8e6ff3c3e21418b28b166e03d6bae00b49316d0f..14c71afe5709b0240c209b9bdfdc88bc0c2e2102 100644
--- a/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-mp-alipay.spec.js
@@ -116,7 +116,7 @@ describe('mp:compiler-mp-alipay', () => {
)
assertCodegen(
'{{getValue(item)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'auto'
@@ -124,7 +124,7 @@ describe('mp:compiler-mp-alipay', () => {
)
assertCodegen(
'{{getValue(item.text)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default").text):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'auto'
diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js
index 8bb05ec7d8bb6fe07022dfd71bd3fba1b8630d3e..3ea32f7f8d27567c9f4c99be1a59b1ecebf67d25 100644
--- a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js
@@ -87,14 +87,14 @@ describe('mp:compiler-mp-baidu', () => {
)
assertCodegen(
'{{getValue(item)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}', {
scopedSlotsCompiler: 'auto'
}
)
assertCodegen(
'{{getValue(item.text)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default").text):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}', {
scopedSlotsCompiler: 'auto'
}
diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js
index bb2785cecbf5b10d9de593c0738e493d4e86bc94..8887702f8e8baa130746fee3446d4d891a027618 100644
--- a/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-mp-weixin.spec.js
@@ -144,7 +144,7 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen(
'{{getValue(item)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'auto'
@@ -152,7 +152,7 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen(
'{{getValue(item.text)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default").text):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'auto'
@@ -219,7 +219,7 @@ describe('mp:compiler-mp-weixin', () => {
it('generate scoped slot with scopedSlotsCompiler: augmented', () => {
assertCodegen(
'{{getValue(item)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'augmented'
@@ -227,7 +227,7 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen(
'{{item}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?$getScopedSlotsParams("551070e6-1","default","item"):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'augmented'
@@ -235,7 +235,7 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen(
'{{getValue(item.text)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default").text):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'augmented'
@@ -243,7 +243,7 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen(
'{{getValue(item)}}',
- '{{$root.m1}}',
+ '{{$root.m1}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-2");var m1=m0?getValue($getScopedSlotsParams("551070e6-2","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
{
scopedSlotsCompiler: 'augmented'
@@ -274,8 +274,8 @@ describe('mp:compiler-mp-weixin', () => {
}
)
assertCodegen(
- '{{item}}{{item}}',
- '{{$root.m1}}{{$root.m3}}',
+ '{{item}}{{item}}',
+ '{{$root.m1}}{{$root.m3}}',
'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?$getScopedSlotsParams("551070e6-1","default","item"):null;var m2=$hasScopedSlotsParams("551070e6-2");var m3=m2?$getScopedSlotsParams("551070e6-2","default","item"):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1,m2:m2,m3:m3}})}',
{
scopedSlotsCompiler: 'augmented'
diff --git a/packages/uni-template-compiler/lib/template/traverse.js b/packages/uni-template-compiler/lib/template/traverse.js
index 43733f7c2277ed18a1af2a286252070e7d084ed2..df256456496a50e2087279f9ef2656d9bcd2e0ee 100644
--- a/packages/uni-template-compiler/lib/template/traverse.js
+++ b/packages/uni-template-compiler/lib/template/traverse.js
@@ -340,19 +340,34 @@ function traverseRenderSlot (callExprNode, state) {
function traverseResolveScopedSlots (callExprNode, state) {
const options = state.options
- function single (children, slotName, ignore) {
- if (Array.isArray(children) && children.length === 1) {
- const child = children[0]
- if (!child.type) {
- return
- }
- if (ignore.includes(child.type)) {
- return single(child.children, slotName, ignore)
+ const vIfAttrName = options.platform.directive + 'if'
+ function single (node, slotName, ignore) {
+ let last = node
+ const vIfs = []
+ function find (children) {
+ if (Array.isArray(children) && children.length === 1) {
+ const child = children[0]
+ if (!child.type) {
+ return
+ }
+ last = child
+ if (child.attr && child.attr[vIfAttrName]) {
+ vIfs.push(child.attr[vIfAttrName])
+ delete child.attr[vIfAttrName]
+ }
+ if (ignore.includes(child.type) && !(child.attr && Object.keys(child.attr).length)) {
+ find(child.children, ignore)
+ }
}
- child.attr = child.attr || {}
- child.attr.slot = slotName
- return true
}
+ find(node.children)
+ last.attr = last.attr || {}
+ last.attr.slot = slotName
+ if (vIfs.length) {
+ // 简易合并
+ last.attr[vIfAttrName] = vIfs.length > 1 ? `{{${vIfs.map(str => str.replace(/^\{\{(.+)\}\}$/, '($1)')).join('&&')}}}` : vIfs[0]
+ }
+ return last
}
return callExprNode.arguments[0].elements.map(slotNode => {
let keyProperty = false
@@ -403,18 +418,11 @@ function traverseResolveScopedSlots (callExprNode, state) {
if (options.scopedSlotsCompiler === 'auto' && slotNode.scopedSlotsCompiler === 'augmented') {
parentNode.attr['scoped-slots-compiler'] = 'augmented'
}
- const children = normalizeChildren(traverseExpr(returnExprNodes, state))
// 除百度、字节外其他小程序仅默认插槽可以支持多个节点
- if (single(children, slotName, ['template', 'block'])) {
- return children[0]
- }
- return {
+ return single({
type: 'block',
- attr: {
- slot: slotName
- },
- children
- }
+ children: normalizeChildren(traverseExpr(returnExprNodes, state))
+ }, slotName, ['template', 'block'])
})
}