diff --git a/packages/uni-cli-shared/src/vue/utils.ts b/packages/uni-cli-shared/src/vue/utils.ts
index f9c94a54d4bb7833a7b2f86dbb740a2a976c62d3..74787ce46afd1a9a49bfef4351bfa085b4c632fb 100644
--- a/packages/uni-cli-shared/src/vue/utils.ts
+++ b/packages/uni-cli-shared/src/vue/utils.ts
@@ -85,7 +85,7 @@ export function addStaticClass(node: ElementNode, clazz: string) {
export function createDirectiveNode(
name: string,
arg: string,
- exp: string | ExpressionNode
+ exp?: string | ExpressionNode
): DirectiveNode {
return {
type: NodeTypes.DIRECTIVE,
diff --git a/packages/uni-mp-alipay/__tests__/vSlot.spec.ts b/packages/uni-mp-alipay/__tests__/vSlot.spec.ts
index d3378167d9003e86d9e4564a8cf9d20c61c51cad..fa3c518b4984d8c00c4bfb5cba0942df8b6b410c 100644
--- a/packages/uni-mp-alipay/__tests__/vSlot.spec.ts
+++ b/packages/uni-mp-alipay/__tests__/vSlot.spec.ts
@@ -4,14 +4,14 @@ describe('mp-alipay: transform v-slot', () => {
test('default slot', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`test`,
- `test`,
+ `test`,
`(_ctx, _cache) => {
return {}
}`
diff --git a/packages/uni-mp-baidu/__tests__/vSlot.spec.ts b/packages/uni-mp-baidu/__tests__/vSlot.spec.ts
index 3871064180797788d1cef66115c6854ab5ea989a..c20001fa64d14b3367472c9237ecf995d55bb946 100644
--- a/packages/uni-mp-baidu/__tests__/vSlot.spec.ts
+++ b/packages/uni-mp-baidu/__tests__/vSlot.spec.ts
@@ -4,14 +4,14 @@ describe('compiler: transform v-slot', () => {
test('default slot', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`test`,
- `test`,
+ `test`,
`(_ctx, _cache) => {
return {}
}`
@@ -27,7 +27,7 @@ describe('compiler: transform v-slot', () => {
)
assert(
`{{error.message}}`,
- `{{v0.c}}`,
+ `{{v0.c}}`,
`(_ctx, _cache) => {
return { a: _w(({ data, loading, error, options }, s0, i0) => { return _e({ a: i0, b: error }, error ? { c: _t(error.message) } : {}); }, { name: 'd', path: 'a', vueId: '2a9ec0b0-0' }) }
}`
diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts
index 7871ac0cf04aaec2f9e0587191ad239699862418..5b6c2049c93684b1507897dd39dd62c7b899ea77 100644
--- a/packages/uni-mp-compiler/__tests__/component.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/component.spec.ts
@@ -10,7 +10,7 @@ describe('compiler: transform component', () => {
test('component + component', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`,
@@ -22,7 +22,7 @@ describe('compiler: transform component', () => {
test('component + component + component', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`,
@@ -56,7 +56,7 @@ describe('compiler: transform component', () => {
test('component + component with v-for', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) }
}`,
@@ -68,7 +68,7 @@ describe('compiler: transform component', () => {
test('component with v-for + component', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
@@ -80,7 +80,7 @@ describe('compiler: transform component', () => {
test('component with v-for + component with v-for', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
diff --git a/packages/uni-mp-compiler/__tests__/slot.spec.ts b/packages/uni-mp-compiler/__tests__/slot.spec.ts
index db2e1460917c34d8ec0e740fad34750559464548..75d0573000541e72199fac4c5cb2fe76067bb178 100644
--- a/packages/uni-mp-compiler/__tests__/slot.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/slot.spec.ts
@@ -4,7 +4,7 @@ describe('compiler: transform slot', () => {
test('basic', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`
@@ -29,7 +29,7 @@ describe('compiler: transform slot', () => {
test('fallback content', () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`
@@ -75,6 +75,13 @@ describe('compiler: transform slot', () => {
``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, index, i0) => { return { a: "d-" + i0, b: _r("d", { key: index }, i0) }; }) }
+}`
+ )
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return { a: _f(_ctx.items, (item, index, i0) => { return { a: "test-" + i0, b: _r("test", { key: index }, i0) }; }) }
}`
)
})
diff --git a/packages/uni-mp-compiler/__tests__/vFor.spec.ts b/packages/uni-mp-compiler/__tests__/vFor.spec.ts
index 0d21865c12fdb135bf1ec4d937e001b91e4ab94f..dfeba59e09149138786a8416b6b25dae2df90472 100644
--- a/packages/uni-mp-compiler/__tests__/vFor.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/vFor.spec.ts
@@ -158,7 +158,7 @@ describe(`compiler: v-for`, () => {
test(`template v-for w/ `, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
}`
@@ -177,7 +177,7 @@ describe(`compiler: v-for`, () => {
test(`v-for on `, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
}`
diff --git a/packages/uni-mp-compiler/__tests__/vIf.spec.ts b/packages/uni-mp-compiler/__tests__/vIf.spec.ts
index a707bcfb7b312d312a6c03ca0f7f090a331fe01c..52d844adb153b4b70f17f8b7d30464033b2d694e 100644
--- a/packages/uni-mp-compiler/__tests__/vIf.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/vIf.spec.ts
@@ -50,7 +50,7 @@ describe(`compiler: v-if`, () => {
test(`template v-if w/ single child`, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
}`
@@ -59,7 +59,7 @@ describe(`compiler: v-if`, () => {
test(`v-if on `, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
}`
diff --git a/packages/uni-mp-compiler/__tests__/vSlot.spec.ts b/packages/uni-mp-compiler/__tests__/vSlot.spec.ts
index 4b1c6f3846c9a5ce6496b5f73db09f15e8bc9aca..85e263e9e4bef35150e276ba9b1aee4ba418a196 100644
--- a/packages/uni-mp-compiler/__tests__/vSlot.spec.ts
+++ b/packages/uni-mp-compiler/__tests__/vSlot.spec.ts
@@ -4,14 +4,21 @@ describe('compiler: transform v-slot', () => {
test('default slot', () => {
assert(
``,
- ``,
+ ``,
+ `(_ctx, _cache) => {
+ return {}
+}`
+ )
+ assert(
+ ``,
+ ``,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`test`,
- `test`,
+ `test`,
`(_ctx, _cache) => {
return {}
}`
diff --git a/packages/uni-mp-compiler/src/transforms/transformSlot.ts b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
index aab34645d91d08f18be1b1f54caee6340a409405..09aa6bba0e93978bc6db0529407745c0bfadcd8a 100644
--- a/packages/uni-mp-compiler/src/transforms/transformSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
@@ -31,6 +31,10 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) {
let hasOtherDir = false
const nonNameProps: (AttributeNode | DirectiveNode)[] = []
const { props } = node
+ // 默认插槽强制设置name
+ if (!findProp(node, 'name')) {
+ props.unshift(createAttributeNode('name', 'default'))
+ }
for (let i = 0; i < props.length; i++) {
const p = props[i]
if (p.type === NodeTypes.ATTRIBUTE) {
@@ -119,24 +123,24 @@ function transformScopedSlotName(
if (!context.miniProgram.slot.dynamicSlotNames) {
return
}
- const { props } = node
const slotKey = parseScopedSlotKey(context)
+ if (!slotKey) {
+ return
+ }
const nameProps = findProp(node, 'name')
- if (!nameProps) {
- // 生成默认的 default 插槽名
- if (slotKey) {
- props.push(
- createBindDirectiveNode(
- 'name',
- rewriteExpression(
- createSimpleExpression(`"${SLOT_DEFAULT_NAME}-"+` + slotKey),
- context
- ).content
- )
+ if (nameProps && nameProps.type === NodeTypes.ATTRIBUTE && nameProps.value) {
+ const { props } = node
+ props.splice(
+ props.indexOf(nameProps),
+ 1,
+ createBindDirectiveNode(
+ 'name',
+ rewriteExpression(
+ createSimpleExpression(`"${nameProps.value.content}-"+` + slotKey),
+ context
+ ).content
)
- } else {
- props.push(createAttributeNode('name', SLOT_DEFAULT_NAME))
- }
+ )
}
}
diff --git a/packages/uni-mp-compiler/src/transforms/vSlot.ts b/packages/uni-mp-compiler/src/transforms/vSlot.ts
index 2f55ec822c05304891c7481a847d99048770cd39..d574e364b5c77a0c472141a51a11d84352446980 100644
--- a/packages/uni-mp-compiler/src/transforms/vSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/vSlot.ts
@@ -15,6 +15,7 @@ import {
createCompoundExpression,
createSimpleExpression,
DirectiveNode,
+ ElementNode,
ElementTypes,
ErrorCodes,
ExpressionNode,
@@ -30,6 +31,7 @@ import {
import { dynamicSlotName, SLOT_DEFAULT_NAME } from '@dcloudio/uni-shared'
import {
createBindDirectiveNode,
+ createDirectiveNode,
isUserComponent,
} from '@dcloudio/uni-cli-shared'
import { WITH_SCOPED_SLOT } from '../runtimeHelpers'
@@ -45,7 +47,7 @@ import {
SCOPED_SLOT_IDENTIFIER,
} from './utils'
import { createVForArrowFunctionExpression } from './vFor'
-import { DYNAMIC_SLOT } from '..'
+import { DYNAMIC_SLOT } from '../runtimeHelpers'
export const transformSlot: NodeTransform = (node, context) => {
if (!isUserComponent(node, context as any)) {
@@ -78,6 +80,10 @@ export const transformSlot: NodeTransform = (node, context) => {
)
break
}
+ if (!slotDir.arg) {
+ // v-slot => v-slot:default
+ slotDir.arg = createSimpleExpression('default', true)
+ }
const slotName = transformTemplateSlotElement(
slotDir,
slotElement,
@@ -97,19 +103,47 @@ export const transformSlot: NodeTransform = (node, context) => {
slots.add(slotName)
}
}
+
+ if (isMiniProgramComponent) {
+ return
+ }
+
if (implicitDefaultChildren.length) {
slots.add(SLOT_DEFAULT_NAME)
}
+
if (onComponentSlot) {
//
- // =>
+ // =>
slots.add(SLOT_DEFAULT_NAME)
const templateNode = createTemplateNode(
onComponentSlot,
implicitDefaultChildren
)
transformTemplateSlotElement(onComponentSlot, templateNode, node, context)
- node.children = templateNode.children
+ node.children = [templateNode]
+ } else {
+ if (implicitDefaultChildren.length) {
+ // test => test
+ const vSlotDir = createDirectiveNode('slot', 'default')
+ const child = implicitDefaultChildren[0] as ElementNode
+ const isSingleElementNode =
+ implicitDefaultChildren.length === 1 &&
+ child.type === NodeTypes.ELEMENT &&
+ !findDir(child, 'for')
+ if (isSingleElementNode) {
+ child.props.unshift(vSlotDir)
+ } else {
+ const templateNode = createTemplateNode(
+ vSlotDir,
+ implicitDefaultChildren
+ )
+ implicitDefaultChildren.forEach((child) => {
+ node.children.splice(node.children.indexOf(child), 1)
+ })
+ node.children.unshift(templateNode)
+ }
+ }
}
// 不支持 $slots, 则自动补充 props
if (slots.size && !context.miniProgram.slot.$slots) {