Skip to content

Commit 571ed42

Browse files
committed
feat(compiler-core/v-on): support @vnode-xxx usage for vnode hooks
1 parent 5495c70 commit 571ed42

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

packages/compiler-core/__tests__/transforms/vOn.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ describe('compiler: transform v-on', () => {
300300
expect(onError).not.toHaveBeenCalled()
301301
})
302302

303+
test('case conversion for vnode hooks', () => {
304+
const { node } = parseWithVOn(`<div v-on:vnode-mounted="onMount"/>`)
305+
const props = (node.codegenNode as CallExpression)
306+
.arguments[1] as ObjectExpression
307+
expect(props.properties[0]).toMatchObject({
308+
key: {
309+
content: `onVnodeMounted`
310+
},
311+
value: {
312+
content: `onMount`
313+
}
314+
})
315+
})
316+
303317
describe('cacheHandler', () => {
304318
test('empty handler', () => {
305319
const { root, node } = parseWithVOn(`<div v-on:click.prevent />`, {

packages/compiler-core/src/transforms/vOn.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
createCompoundExpression,
99
SimpleExpressionNode
1010
} from '../ast'
11-
import { capitalize } from '@vue/shared'
11+
import { capitalize, camelize } from '@vue/shared'
1212
import { createCompilerError, ErrorCodes } from '../errors'
1313
import { processExpression } from './transformExpression'
1414
import { isMemberExpression, hasScopeRef } from '../utils'
@@ -38,11 +38,12 @@ export const transformOn: DirectiveTransform = (
3838
let eventName: ExpressionNode
3939
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
4040
if (arg.isStatic) {
41-
eventName = createSimpleExpression(
42-
`on${capitalize(arg.content)}`,
43-
true,
44-
arg.loc
45-
)
41+
const rawName = arg.content
42+
// for @vnode-xxx event listeners, auto convert it to camelCase
43+
const normalizedName = rawName.startsWith(`vnode`)
44+
? capitalize(camelize(rawName))
45+
: capitalize(rawName)
46+
eventName = createSimpleExpression(`on${normalizedName}`, true, arg.loc)
4647
} else {
4748
eventName = createCompoundExpression([`"on" + (`, arg, `)`])
4849
}

0 commit comments

Comments
 (0)