diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
index 5db21b7a19d93aff1c6977a89a6706c47d33ed8d..9538ae89c32c78a98a767e7931a1e03f8e5fd6a6 100644
--- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
@@ -136,6 +136,12 @@ describe('codegen', () => {
'',
`with(this){return _c('view',{attrs:{"data-b":_$s(0,'a-data-b',b),"_i":0}})}`
)
+ })
+ it('generate v-if directive', () => {
+ assertCodegen(
+ '123d',
+ `with(this){return (_$s(0,'i',a))?_c('text'):(_$s(1,'e',b))?_c('text'):(_$s(2,'e',c))?_c('text'):_c('text')}`
+ )
})
})
/* eslint-enable quotes */
diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
index 00e7dbfe705d0af113792d66977afea500b4e3b1..23b14375a49b1835c5099f1f13ced7e7fc04b59e 100644
--- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
@@ -75,6 +75,12 @@ describe('codegen', () => {
'',
`with(this){return _c('v-uni-view',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}`
)
+ })
+ it('generate v-if directive', () => {
+ assertCodegen(
+ '123d',
+ `with(this){return (_$g(0,'i'))?_c('v-uni-text',{attrs:{"_i":0}},[_v("1")]):(_$g(1,'e'))?_c('v-uni-text',{attrs:{"_i":1}},[_v("2")]):(_$g(2,'e'))?_c('v-uni-text',{attrs:{"_i":2}},[_v("3")]):_c('v-uni-text',{attrs:{"_i":3}},[_v("d")])}`
+ )
})
})
/* eslint-enable quotes */
diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js
index 88d2fae464a58d3e56098a173f576939f9fdc62d..d97e014ea4d8fa52ec9c55acda752392a95020b4 100644
--- a/packages/uni-template-compiler/__tests__/demo.js
+++ b/packages/uni-template-compiler/__tests__/demo.js
@@ -18,13 +18,8 @@ const scopedPath = path.resolve(__dirname, '../../')
const compiler = require('../lib')
const res = compiler.compile(
- `
-
-
-
-
-
-
+ `
+123d
`, {
miniprogram: true,
resourcePath: '/User/fxy/Documents/test.wxml',
@@ -37,9 +32,9 @@ const res = compiler.compile(
mp: {
platform: 'mp-weixin'
},
- filterModules: ['swipe']
+ filterModules: ['swipe'],
// service: true,
- // view: true
+ view: true
})
console.log(require('util').inspect(res, {
diff --git a/packages/uni-template-compiler/lib/app/parser/base-parser.js b/packages/uni-template-compiler/lib/app/parser/base-parser.js
index 30fe8da69b31c38529317bd2804d04c593cff67d..538e294df78e8354f065a60ed1fc45a49139adab 100644
--- a/packages/uni-template-compiler/lib/app/parser/base-parser.js
+++ b/packages/uni-template-compiler/lib/app/parser/base-parser.js
@@ -18,6 +18,10 @@ function parseIs (el, genVar) {
}
}
+function isProcessed (exp) {
+ return String(exp).indexOf('_$') === 0
+}
+// 当根节点是由if,elseif,else组成,会调用多次parseIf来解析root
function parseIf (el, createGenVar, isScopedSlot) {
if (!el.if) {
return
@@ -26,11 +30,13 @@ function parseIf (el, createGenVar, isScopedSlot) {
isScopedSlot = false
}
el.ifConditions.forEach(con => {
- if (isVar(con.exp)) {
+ if (!isProcessed(con.exp) && isVar(con.exp)) {
con.exp = createGenVar(con.block.attrsMap[ID], isScopedSlot)(con.block.elseif ? V_ELSE_IF : V_IF, con.exp)
}
})
- el.if = createGenVar(el.attrsMap[ID], isScopedSlot)(V_IF, el.if)
+ if (!isProcessed(el.if)) {
+ el.if = createGenVar(el.attrsMap[ID], isScopedSlot)(V_IF, el.if)
+ }
}
function parseFor (el, createGenVar, isScopedSlot, fill = false) {
diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js
index fc46da0508fc97543b31242a5b963d4ecafed32f..f7cfad5ff55c83f30b7459711d43a95ea9ddb325 100644
--- a/packages/uni-template-compiler/lib/app/service.js
+++ b/packages/uni-template-compiler/lib/app/service.js
@@ -156,6 +156,11 @@ function transformNode (el, parent, state, isScopedSlot) {
function postTransformNode (el, options) {
if (!el.parent) { // 从根节点开始递归处理
+ if (options.root) { // 当根节点是由if,elseif,else组成
+ parseIf(options.root, createGenVar)
+ } else {
+ options.root = el
+ }
traverseNode(el, false, {
forIteratorId: 0,
transformNode,
diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js
index 46242f9206d9b4db36ea93cc9408398eec7c041d..7f7f179e405fc12b7356254d27b62e76ce25e0d2 100644
--- a/packages/uni-template-compiler/lib/app/view.js
+++ b/packages/uni-template-compiler/lib/app/view.js
@@ -155,6 +155,11 @@ function transformNode (el, parent, state, isScopedSlot) {
function postTransformNode (el, options) {
if (!el.parent) { // 从根节点开始递归处理
+ if (options.root) { // 当根节点是由if,elseif,else组成
+ parseIf(options.root, createGenVar)
+ } else {
+ options.root = el
+ }
traverseNode(el, false, {
forIteratorId: 0,
transformNode,
@@ -235,4 +240,4 @@ module.exports = {
},
postTransformNode,
genData
-}
+}