diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
index 3d494d107faad6bf1b615457baf8e1719a0c4d3f..71c08eb11b84d1d65a498b21c8490c6afbe3d133 100644
--- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
@@ -251,7 +251,11 @@ describe('mp:compiler-extra', () => {
)
})
- it('generate events inside v-for', () => {
+ it('generate events inside v-for', () => {
+ assertCodegen(
+ ``,
+ ``
+ )
// TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
assertCodegen(
`1count++">`,
diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js
index 5317a4b4beed2c8ce378e185a4ada08e805242e5..cfca7bbdc6798af3c887323f1b7ce7eb02a79382 100644
--- a/packages/uni-template-compiler/__tests__/demo.js
+++ b/packages/uni-template-compiler/__tests__/demo.js
@@ -2,7 +2,7 @@ const compiler = require('../lib')
const res = compiler.compile(
`
-{{t.a}}{{t['a']}}{{t.a(b)}}{{t['a'](b)}}{{u.t.a(b)}}{{u.t.a}}
+
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
mp: {
diff --git a/packages/uni-template-compiler/lib/script/traverse/data/event.js b/packages/uni-template-compiler/lib/script/traverse/data/event.js
index 10b71be3ee5b779e600591a3f8dffdbc9a7a5a52..22ec016c584cf90780417d629f5fe469ff97f179 100644
--- a/packages/uni-template-compiler/lib/script/traverse/data/event.js
+++ b/packages/uni-template-compiler/lib/script/traverse/data/event.js
@@ -167,6 +167,39 @@ function getMethodName (methodName) {
return methodName === '__HOLDER__' ? '' : methodName
}
+function parseEventByCallExpression (callExpr, methods) {
+ let methodName = callExpr.callee.name
+ if (methodName === '$set') {
+ methodName = INTERNAL_SET_SYNC
+ }
+ const arrayExpression = [t.stringLiteral(getMethodName(methodName))]
+ const args = callExpr.arguments
+ if (methodName === INTERNAL_SET_SYNC) {
+ // v-bind:title.sync="doc.title"
+ // ['$set',['doc.a','title','$event']]
+ const argsExpression = []
+ argsExpression.push(
+ t.memberExpression(args[0], t.identifier(args[1].value))
+ )
+ argsExpression.push(t.stringLiteral(args[1].value))
+ argsExpression.push(t.stringLiteral('$event'))
+ arrayExpression.push(t.arrayExpression(argsExpression))
+ } else {
+ if (args.length) {
+ const argsExpression = []
+ args.forEach(arg => {
+ if (t.isIdentifier(arg) && arg.name === '$event') {
+ argsExpression.push(t.stringLiteral('$event'))
+ } else {
+ argsExpression.push(arg)
+ }
+ })
+ arrayExpression.push(t.arrayExpression(argsExpression))
+ }
+ }
+ methods.push(t.arrayExpression(arrayExpression))
+}
+
function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, tagName, ret) {
const key = keyPath.node
let type = key.value || key.name
@@ -235,7 +268,22 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
methods.push(addEventExpressionStatement(funcPath, state, isCustom))
} else {
let anonymous = true
- funcPath.traverse({
+
+ // "click":function($event) {click1(item);click2(item);}
+ const body = funcPath.node.body.body
+ if (body.length) {
+ const exprStatements = body.filter(node => {
+ return t.isExpressionStatement(node) && t.isCallExpression(node.expression)
+ })
+ if (exprStatements.length === body.length) {
+ anonymous = false
+ exprStatements.forEach(exprStatement => {
+ parseEventByCallExpression(exprStatement.expression, methods)
+ })
+ }
+ }
+
+ anonymous && funcPath.traverse({
noScope: true,
MemberExpression (path) {
if (path.node.object.name === '$event' && path.node.property.name ===
@@ -271,36 +319,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
if (t.isCallExpression(argument)) {
if (t.isIdentifier(argument.callee)) {
anonymous = false
- let methodName = argument.callee.name
- if (methodName === '$set') {
- methodName = INTERNAL_SET_SYNC
- }
- const arrayExpression = [t.stringLiteral(getMethodName(methodName))]
- const args = argument.arguments
- if (methodName === INTERNAL_SET_SYNC) {
- // v-bind:title.sync="doc.title"
- // ['$set',['doc.a','title','$event']]
- const argsExpression = []
- argsExpression.push(
- t.memberExpression(args[0], t.identifier(args[1].value))
- )
- argsExpression.push(t.stringLiteral(args[1].value))
- argsExpression.push(t.stringLiteral('$event'))
- arrayExpression.push(t.arrayExpression(argsExpression))
- } else {
- if (args.length) {
- const argsExpression = []
- args.forEach(arg => {
- if (t.isIdentifier(arg) && arg.name === '$event') {
- argsExpression.push(t.stringLiteral('$event'))
- } else {
- argsExpression.push(arg)
- }
- })
- arrayExpression.push(t.arrayExpression(argsExpression))
- }
- }
- methods.push(t.arrayExpression(arrayExpression))
+ parseEventByCallExpression(argument, methods)
}
}
}