提交 89184583 编写于 作者: Q qiang

fix(mp): named slot with v-if fixed #2635

上级 66d611b2
...@@ -274,6 +274,29 @@ describe('mp:compiler-extra', () => { ...@@ -274,6 +274,29 @@ describe('mp:compiler-extra', () => {
) )
}) })
it('generate v-slot with v-if', () => {
assertCodegen(
'<custom-view><template v-if="show">hello</template></custom-view>',
'<custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}"><block wx:if="{{show}}">hello</block></custom-view>'
)
assertCodegen(
'<custom-view><template v-if="show" #name>hello</template></custom-view>',
'<custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'name\']}}"><view slot="name" wx:if="{{show}}">hello</view></custom-view>'
)
assertCodegen(
'<custom-view><template v-if="show" #name><text>hello</text></template></custom-view>',
'<custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'name\']}}"><text slot="name" wx:if="{{show}}">hello</text></custom-view>'
)
assertCodegen(
'<custom-view><template v-if="show" #name><view>hello</view></template></custom-view>',
'<custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'name\']}}"><view slot="name" wx:if="{{show}}">hello</view></custom-view>'
)
assertCodegen(
'<custom-view><template v-if="show" #name><view v-if="test1||test2">hello</view></template></custom-view>',
'<custom-view vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'name\']}}"><view slot="name" wx:if="{{(show)&&(test1||test2)}}">hello</view></custom-view>'
)
})
it('generate events inside v-for', () => { it('generate events inside v-for', () => {
assertCodegen( assertCodegen(
'<view v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/>', '<view v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/>',
......
...@@ -51,7 +51,12 @@ function replaceId (path, ids) { ...@@ -51,7 +51,12 @@ function replaceId (path, ids) {
} }
module.exports = function getResolveScopedSlots (parent, state) { 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 fn = properties.find(path => path.get('key').isIdentifier({ name: 'fn' }))
const params = fn.get('value.params.0') const params = fn.get('value.params.0')
if (!params) { if (!params) {
...@@ -83,7 +88,7 @@ module.exports = function getResolveScopedSlots (parent, state) { ...@@ -83,7 +88,7 @@ module.exports = function getResolveScopedSlots (parent, state) {
const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId]) const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId])
orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))])) orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))]))
// scopedSlotsCompiler auto // scopedSlotsCompiler auto
parent.get('arguments.0.elements.0').node.scopedSlotsCompiler = 'augmented' objectPath.node.scopedSlotsCompiler = 'augmented'
} }
} }
} }
...@@ -341,9 +341,9 @@ function traverseRenderSlot (callExprNode, state) { ...@@ -341,9 +341,9 @@ function traverseRenderSlot (callExprNode, state) {
function traverseResolveScopedSlots (callExprNode, state) { function traverseResolveScopedSlots (callExprNode, state) {
const options = state.options const options = state.options
const vIfAttrName = options.platform.directive + 'if' const vIfAttrName = options.platform.directive + 'if'
function single (node, slotName, ignore) { function single (node, slotName, vIfCode, ignore) {
let last = node let last = node
const vIfs = [] const vIfs = vIfCode ? [vIfCode] : []
function find (children) { function find (children) {
if (Array.isArray(children) && children.length === 1) { if (Array.isArray(children) && children.length === 1) {
const child = children[0] const child = children[0]
...@@ -373,6 +373,12 @@ function traverseResolveScopedSlots (callExprNode, state) { ...@@ -373,6 +373,12 @@ function traverseResolveScopedSlots (callExprNode, state) {
let keyProperty = false let keyProperty = false
let fnProperty = false let fnProperty = false
let proxyProperty = false let proxyProperty = false
let vIfCode
// TODO v-else
if (t.isConditionalExpression(slotNode)) {
vIfCode = genCode(slotNode.test)
slotNode = slotNode.consequent
}
slotNode.properties.forEach(property => { slotNode.properties.forEach(property => {
switch (property.key.name) { switch (property.key.name) {
case 'key': case 'key':
...@@ -422,7 +428,7 @@ function traverseResolveScopedSlots (callExprNode, state) { ...@@ -422,7 +428,7 @@ function traverseResolveScopedSlots (callExprNode, state) {
return single({ return single({
type: 'block', type: 'block',
children: normalizeChildren(traverseExpr(returnExprNodes, state)) children: normalizeChildren(traverseExpr(returnExprNodes, state))
}, slotName, ['template', 'block']) }, slotName, vIfCode, ['template', 'block'])
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册