|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/v-on-event-hyphenation |
| 5 | +description: enforce v-on event naming style on custom components in template |
| 6 | +--- |
| 7 | +# vue/v-on-event-hyphenation |
| 8 | + |
| 9 | +> enforce v-on event naming style on custom components in template |
| 10 | +
|
| 11 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> |
| 12 | +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule enforces using hyphenated v-on event names on custom components in Vue templates. |
| 17 | + |
| 18 | +<eslint-code-block fix :rules="{'vue/v-on-event-hyphenation': ['error', 'always', { autofix: true }]}"> |
| 19 | + |
| 20 | +```vue |
| 21 | +<template> |
| 22 | + <!-- ✓ GOOD --> |
| 23 | + <MyComponent v-on:custom-event="handleEvent"/> |
| 24 | + <MyComponent @custom-event="handleEvent"/> |
| 25 | +
|
| 26 | + <!-- ✗ BAD --> |
| 27 | + <MyComponent v-on:customEvent="handleEvent"/> |
| 28 | + <MyComponent @customEvent="handleEvent"/> |
| 29 | +</template> |
| 30 | +``` |
| 31 | + |
| 32 | +</eslint-code-block> |
| 33 | + |
| 34 | +## :wrench: Options |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "vue/v-on-event-hyphenation": ["error", "always" | "never", { |
| 39 | + "autofix": false, |
| 40 | + "ignore": [] |
| 41 | + }] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +- `"always"` (default) ... Use hyphenated name. |
| 46 | +- `"never"` ... Don't use hyphenated name. |
| 47 | +- `"ignore"` ... Array of ignored names |
| 48 | +- `"autofix"` ... If `true`, enable autofix. If you are using Vue 2, we recommend that you do not use it due to its side effects. |
| 49 | + |
| 50 | +### `"always"` |
| 51 | + |
| 52 | +It errors on upper case letters. |
| 53 | + |
| 54 | +<eslint-code-block fix :rules="{'vue/v-on-event-hyphenation': ['error', 'always', { autofix: true }]}"> |
| 55 | + |
| 56 | +```vue |
| 57 | +<template> |
| 58 | + <!-- ✓ GOOD --> |
| 59 | + <MyComponent v-on:custom-event="handleEvent"/> |
| 60 | +
|
| 61 | + <!-- ✗ BAD --> |
| 62 | + <MyComponent v-on:customEvent="handleEvent"/> |
| 63 | +</template> |
| 64 | +``` |
| 65 | + |
| 66 | +</eslint-code-block> |
| 67 | + |
| 68 | +### `"never"` |
| 69 | + |
| 70 | +It errors on hyphens. |
| 71 | + |
| 72 | +<eslint-code-block fix :rules="{'vue/v-on-event-hyphenation': ['error', 'never', { autofix: true }]}"> |
| 73 | + |
| 74 | +```vue |
| 75 | +<template> |
| 76 | + <!-- ✓ GOOD --> |
| 77 | + <MyComponent v-on:customEvent="handleEvent"/> |
| 78 | +
|
| 79 | + <!-- ✗ BAD --> |
| 80 | + <MyComponent v-on:custom-event="handleEvent"/> |
| 81 | +</template> |
| 82 | +``` |
| 83 | + |
| 84 | +</eslint-code-block> |
| 85 | + |
| 86 | +### `"never", { "ignore": ["custom-event"] }` |
| 87 | + |
| 88 | +Don't use hyphenated name but allow custom event names |
| 89 | + |
| 90 | +<eslint-code-block fix :rules="{'vue/v-on-event-hyphenation': ['error', 'never', { ignore: ['custom-event'], autofix: true }]}"> |
| 91 | + |
| 92 | +```vue |
| 93 | +<template> |
| 94 | + <!-- ✓ GOOD --> |
| 95 | + <MyComponent v-on:custom-event="handleEvent"/> |
| 96 | + <MyComponent v-on:myEvent="handleEvent"/> |
| 97 | +
|
| 98 | + <!-- ✗ BAD --> |
| 99 | + <MyComponent v-on:my-event="handleEvent"/> |
| 100 | +</template> |
| 101 | +``` |
| 102 | + |
| 103 | +</eslint-code-block> |
| 104 | + |
| 105 | +## :books: Further Reading |
| 106 | + |
| 107 | +- [Guide - Custom Events] |
| 108 | + |
| 109 | +[Guide - Custom Events]: https://v3.vuejs.org/guide/component-custom-events.html |
| 110 | + |
| 111 | +## :couple: Related Rules |
| 112 | + |
| 113 | +- [vue/custom-event-name-casing](./custom-event-name-casing.md) |
| 114 | +- [vue/attribute-hyphenation](./attribute-hyphenation.md) |
| 115 | + |
| 116 | +## :mag: Implementation |
| 117 | + |
| 118 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/v-on-event-hyphenation.js) |
| 119 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/v-on-event-hyphenation.js) |
0 commit comments