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

fix(mp): v-if + v-for

上级 92be62f5
...@@ -204,7 +204,7 @@ describe(`compiler: v-for`, () => { ...@@ -204,7 +204,7 @@ describe(`compiler: v-for`, () => {
test(`v-if + v-for`, () => { test(`v-if + v-for`, () => {
assert( assert(
`<view v-if="ok" v-for="i in list"/>`, `<view v-if="ok" v-for="i in list"/>`,
`<view wx:if="{{a}}" wx:for="{{b}}" wx:for-item="i"/>`, `<block wx:if="{{a}}"><view wx:for="{{b}}" wx:for-item="i"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {}) return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
}` }`
...@@ -214,7 +214,7 @@ describe(`compiler: v-for`, () => { ...@@ -214,7 +214,7 @@ describe(`compiler: v-for`, () => {
test(`v-if + v-for on <template>`, () => { test(`v-if + v-for on <template>`, () => {
assert( assert(
`<template v-if="ok" v-for="i in list"/>`, `<template v-if="ok" v-for="i in list"/>`,
`<block wx:if="{{a}}" wx:for="{{b}}" wx:for-item="i"/>`, `<block wx:if="{{a}}"><block wx:for="{{b}}" wx:for-item="i"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {}) return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
}` }`
......
...@@ -270,8 +270,11 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) { ...@@ -270,8 +270,11 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) {
tag = hyphenate(tag) tag = hyphenate(tag)
} }
const { push } = context const { push } = context
push(`<${tag}`)
if (isIfElementNode(node)) { const hasVIf = isIfElementNode(node)
const hasVFor = isForElementNode(node)
const hasVIfAndVFor = hasVIf && hasVFor
function genVIfCode(node: IfElementNode) {
const { name, condition } = node.vIf const { name, condition } = node.vIf
if (name === 'if') { if (name === 'if') {
genVIf(condition!, context) genVIf(condition!, context)
...@@ -281,7 +284,18 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) { ...@@ -281,7 +284,18 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) {
genVElse(context) genVElse(context)
} }
} }
if (isForElementNode(node)) { // 小程序中 wx:else wx:elif 不支持与 wx:for 同时使用
// 故 if 需要补充一层 block
if (hasVIfAndVFor) {
push(`<block`)
genVIfCode(node)
push(`>`)
}
push(`<${tag}`)
if (!hasVIfAndVFor && hasVIf) {
genVIfCode(node)
}
if (hasVFor) {
genVFor(node, context) genVFor(node, context)
} }
if (props.length) { if (props.length) {
...@@ -297,6 +311,9 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) { ...@@ -297,6 +311,9 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) {
}) })
push(`</${tag}>`) push(`</${tag}>`)
} }
if (hasVIfAndVFor) {
push(`</block>`)
}
} }
export function genElementProps( export function genElementProps(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册