Browse Source

fix v-on inline function expression with modifiers (fix #5120)

Evan You 9 years ago
parent
commit
a524919d21

+ 15 - 6
src/compiler/codegen/events.js

@@ -48,12 +48,19 @@ function genHandler (
 ): string {
 ): string {
   if (!handler) {
   if (!handler) {
     return 'function(){}'
     return 'function(){}'
-  } else if (Array.isArray(handler)) {
+  }
+
+  if (Array.isArray(handler)) {
     return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
     return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
-  } else if (!handler.modifiers) {
-    return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
+  }
+
+  const isMethodPath = simplePathRE.test(handler.value)
+  const isFunctionExpression = fnExpRE.test(handler.value)
+
+  if (!handler.modifiers) {
+    return isMethodPath || isFunctionExpression
       ? handler.value
       ? handler.value
-      : `function($event){${handler.value}}`
+      : `function($event){${handler.value}}` // inline statement
   } else {
   } else {
     let code = ''
     let code = ''
     const keys = []
     const keys = []
@@ -71,9 +78,11 @@ function genHandler (
     if (keys.length) {
     if (keys.length) {
       code += genKeyFilter(keys)
       code += genKeyFilter(keys)
     }
     }
-    const handlerCode = simplePathRE.test(handler.value)
+    const handlerCode = isMethodPath
       ? handler.value + '($event)'
       ? handler.value + '($event)'
-      : handler.value
+      : isFunctionExpression
+        ? `(${handler.value})($event)`
+        : handler.value
     return `function($event){${code}${handlerCode}}`
     return `function($event){${code}${handlerCode}}`
   }
   }
 }
 }

+ 1 - 0
src/compiler/codegen/index.js

@@ -40,6 +40,7 @@ export function generate (
   const code = ast ? genElement(ast) : '_c("div")'
   const code = ast ? genElement(ast) : '_c("div")'
   staticRenderFns = prevStaticRenderFns
   staticRenderFns = prevStaticRenderFns
   onceCount = prevOnceCount
   onceCount = prevOnceCount
+  console.log(code)
   return {
   return {
     render: `with(this){return ${code}}`,
     render: `with(this){return ${code}}`,
     staticRenderFns: currentStaticRenderFns
     staticRenderFns: currentStaticRenderFns

+ 5 - 0
test/unit/modules/compiler/codegen.spec.js

@@ -360,6 +360,11 @@ describe('codegen', () => {
       '<input @input="e=>current++">',
       '<input @input="e=>current++">',
       `with(this){return _c('input',{on:{"input":e=>current++}})}`
       `with(this){return _c('input',{on:{"input":e=>current++}})}`
     )
     )
+    // with modifiers
+    assertCodegen(
+      `<input @keyup.enter="e=>current++">`,
+      `with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;(e=>current++)($event)}}})}`
+    )
   })
   })
 
 
   // #3893
   // #3893