提交 ff89c56c 编写于 作者: fxy060608's avatar fxy060608

fix(mp): component with v-else (#3096)

上级 238dae0f
......@@ -104,9 +104,17 @@ describe('compiler: transform component', () => {
`<uni-collapse ref="a" ref="{{a}}" slot="c" slot="{{b}}" class="{{['e', c]}}" style="{{'g' + ';' + d}}" bindclick="{{e}}" data-a="n" data-b="{{f}}" key="p" key="{{g}}" is="s" is="{{h}}" u-i="2a9ec0b0-0" bindupdateFirst="{{i}}" bindupdateLast="{{j}}" u-p="{{k}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.b, b: _ctx.d, c: _n(_ctx.f), d: _s(_ctx.h), e: _o(_ctx.i), f: _ctx.o, g: _ctx.r, h: _ctx.t, i: _o($event => _ctx.j = $event), j: _o($event => _ctx.k = $event), k: _p({ ['prop-a']: 'l', ['prop-b']: _ctx.m, first: _ctx.j, last: _ctx.k }) }
}`
)
assert(
`<uni-collapse v-if="ok" :accordion="true"/><uni-collapse v-else :accordion="true"/>`,
`<uni-collapse wx:if="{{a}}" u-i="2a9ec0b0-0" u-p="{{b}}"/><uni-collapse wx:else u-i="2a9ec0b0-1" u-p="{{c||''}}"/>`,
`(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? { b: _p({ accordion: true }) } : { c: _p({ accordion: true }) })
}`
)
})
test(`mini program component`, () => {
const filename = 'pages/vant/vant'
addMiniProgramPageJson(filename, {
......
......@@ -179,7 +179,7 @@ describe(`compiler: v-if`, () => {
test(`v-for + v-if + v-else`, () => {
assert(
`<view v-for="item in items"><uni-icons v-if="ok"/><uni-icons v-else :title="item.title"/></view>`,
`<view wx:for="{{a}}" wx:for-item="item"><uni-icons wx:if="{{b}}" u-i="{{item.a}}"/><uni-icons wx:else u-i="{{item.b}}" u-p="{{item.c}}"/></view>`,
`<view wx:for="{{a}}" wx:for-item="item"><uni-icons wx:if="{{b}}" u-i="{{item.a}}"/><uni-icons wx:else u-i="{{item.b}}" u-p="{{item.c||''}}"/></view>`,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return _ctx.ok ? { a: '2a9ec0b0-0' + '-' + i0 } : { b: '2a9ec0b0-1' + '-' + i0, c: _p({ title: item.title }) }; }), b: _ctx.ok }
}`
......
......@@ -3,6 +3,7 @@ import {
ComponentNode,
createSimpleExpression,
DirectiveNode,
ElementNode,
isSimpleIdentifier,
isStaticExp,
NodeTypes,
......@@ -34,6 +35,7 @@ import {
} from '@babel/types'
import { RENDER_PROPS } from '../runtimeHelpers'
import { parseExpr } from '../ast'
import { isIfElementNode } from './vIf'
export const transformComponent: NodeTransform = (node, context) => {
if (!isUserComponent(node, context as any)) {
......@@ -229,6 +231,7 @@ export function isPropsBinding({ arg }: DirectiveNode) {
export function rewritePropsBinding(
dir: DirectiveNode,
node: ElementNode,
context: TransformContext
) {
dir.exp = createSimpleExpression(
......@@ -239,6 +242,6 @@ export function rewritePropsBinding(
dir.loc,
context
)!
)
) + (isIfElementNode(node) && node.vIf.name === 'else' ? `||''` : '')
)
}
......@@ -81,7 +81,7 @@ export const transformIdentifier: NodeTransform = (node, context) => {
hasStyleBinding = true
rewriteStyle(i, dir, props, context)
} else if (isPropsBinding(dir)) {
rewritePropsBinding(dir, context)
rewritePropsBinding(dir, node, context)
} else {
dir.exp = rewriteExpression(exp, context)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册