Skip to content

Commit a524919

Browse files
committed
fix v-on inline function expression with modifiers (fix vuejs#5120)
1 parent 2afaac2 commit a524919

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/compiler/codegen/events.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ function genHandler (
4848
): string {
4949
if (!handler) {
5050
return 'function(){}'
51-
} else if (Array.isArray(handler)) {
51+
}
52+
53+
if (Array.isArray(handler)) {
5254
return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
53-
} else if (!handler.modifiers) {
54-
return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
55+
}
56+
57+
const isMethodPath = simplePathRE.test(handler.value)
58+
const isFunctionExpression = fnExpRE.test(handler.value)
59+
60+
if (!handler.modifiers) {
61+
return isMethodPath || isFunctionExpression
5562
? handler.value
56-
: `function($event){${handler.value}}`
63+
: `function($event){${handler.value}}` // inline statement
5764
} else {
5865
let code = ''
5966
const keys = []
@@ -71,9 +78,11 @@ function genHandler (
7178
if (keys.length) {
7279
code += genKeyFilter(keys)
7380
}
74-
const handlerCode = simplePathRE.test(handler.value)
81+
const handlerCode = isMethodPath
7582
? handler.value + '($event)'
76-
: handler.value
83+
: isFunctionExpression
84+
? `(${handler.value})($event)`
85+
: handler.value
7786
return `function($event){${code}${handlerCode}}`
7887
}
7988
}

src/compiler/codegen/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function generate (
4040
const code = ast ? genElement(ast) : '_c("div")'
4141
staticRenderFns = prevStaticRenderFns
4242
onceCount = prevOnceCount
43+
console.log(code)
4344
return {
4445
render: `with(this){return ${code}}`,
4546
staticRenderFns: currentStaticRenderFns

test/unit/modules/compiler/codegen.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ describe('codegen', () => {
360360
'<input @input="e=>current++">',
361361
`with(this){return _c('input',{on:{"input":e=>current++}})}`
362362
)
363+
// with modifiers
364+
assertCodegen(
365+
`<input @keyup.enter="e=>current++">`,
366+
`with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;(e=>current++)($event)}}})}`
367+
)
363368
})
364369

365370
// #3893

0 commit comments

Comments
 (0)