diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index 8d201bc4ccf2df2d4b78706a3a31eac5341a4cc6..4dcb81d258f4e7bd46794c2f57a59f7669151013 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -274,6 +274,29 @@ describe('mp:compiler-extra', () => { ) }) + it('generate v-slot with v-if', () => { + assertCodegen( + '', + 'hello' + ) + assertCodegen( + '', + 'hello' + ) + assertCodegen( + '', + 'hello' + ) + assertCodegen( + '', + 'hello' + ) + assertCodegen( + '', + 'hello' + ) + }) + it('generate events inside v-for', () => { assertCodegen( '', diff --git a/packages/uni-template-compiler/lib/script/traverse/resolve-scoped-slots.js b/packages/uni-template-compiler/lib/script/traverse/resolve-scoped-slots.js index 77284f67ebb234c3e0bb609279f8b2c2c6e66ab3..8b12d66d058a218a2579e304f336f54528d20618 100644 --- a/packages/uni-template-compiler/lib/script/traverse/resolve-scoped-slots.js +++ b/packages/uni-template-compiler/lib/script/traverse/resolve-scoped-slots.js @@ -51,7 +51,12 @@ function replaceId (path, ids) { } module.exports = function getResolveScopedSlots (parent, state) { - const properties = parent.get('arguments.0.elements.0.properties') + let objectPath = parent.get('arguments.0.elements.0') + // TODO v-else + if (objectPath.isConditionalExpression()) { + objectPath = objectPath.get('consequent') + } + const properties = objectPath.get('properties') const fn = properties.find(path => path.get('key').isIdentifier({ name: 'fn' })) const params = fn.get('value.params.0') if (!params) { @@ -83,7 +88,7 @@ module.exports = function getResolveScopedSlots (parent, state) { const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId]) orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))])) // scopedSlotsCompiler auto - parent.get('arguments.0.elements.0').node.scopedSlotsCompiler = 'augmented' + objectPath.node.scopedSlotsCompiler = 'augmented' } } } diff --git a/packages/uni-template-compiler/lib/template/traverse.js b/packages/uni-template-compiler/lib/template/traverse.js index df256456496a50e2087279f9ef2656d9bcd2e0ee..25acfbc3559bbb7ea7307f12c1960d263c6c9691 100644 --- a/packages/uni-template-compiler/lib/template/traverse.js +++ b/packages/uni-template-compiler/lib/template/traverse.js @@ -341,9 +341,9 @@ function traverseRenderSlot (callExprNode, state) { function traverseResolveScopedSlots (callExprNode, state) { const options = state.options const vIfAttrName = options.platform.directive + 'if' - function single (node, slotName, ignore) { + function single (node, slotName, vIfCode, ignore) { let last = node - const vIfs = [] + const vIfs = vIfCode ? [vIfCode] : [] function find (children) { if (Array.isArray(children) && children.length === 1) { const child = children[0] @@ -373,6 +373,12 @@ function traverseResolveScopedSlots (callExprNode, state) { let keyProperty = false let fnProperty = false let proxyProperty = false + let vIfCode + // TODO v-else + if (t.isConditionalExpression(slotNode)) { + vIfCode = genCode(slotNode.test) + slotNode = slotNode.consequent + } slotNode.properties.forEach(property => { switch (property.key.name) { case 'key': @@ -422,7 +428,7 @@ function traverseResolveScopedSlots (callExprNode, state) { return single({ type: 'block', children: normalizeChildren(traverseExpr(returnExprNodes, state)) - }, slotName, ['template', 'block']) + }, slotName, vIfCode, ['template', 'block']) }) }