|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/v-on-handler-style |
| 5 | +description: enforce writing style for handlers in `v-on` directives |
| 6 | +--- |
| 7 | +# vue/v-on-handler-style |
| 8 | + |
| 9 | +> enforce writing style for handlers in `v-on` directives |
| 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 aims to enforce a consistent style in `v-on` event handlers: |
| 17 | + |
| 18 | +```vue |
| 19 | +<!-- Method handler: --> |
| 20 | +<button v-on:click="handler" /> |
| 21 | +
|
| 22 | +<!-- Inline handler: --> |
| 23 | +<button v-on:click="handler()" /> |
| 24 | +
|
| 25 | +<!-- Inline function: --> |
| 26 | +<button v-on:click="() => handler()" /> |
| 27 | +``` |
| 28 | + |
| 29 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error']}"> |
| 30 | + |
| 31 | +```vue |
| 32 | +<template> |
| 33 | + <!-- ✓ GOOD --> |
| 34 | + <button v-on:click="handler" /> |
| 35 | +
|
| 36 | + <!-- ✗ BAD --> |
| 37 | + <button v-on:click="handler()" /> |
| 38 | + <button v-on:click="() => handler()" /> |
| 39 | +</template> |
| 40 | +``` |
| 41 | + |
| 42 | +</eslint-code-block> |
| 43 | + |
| 44 | +## :wrench: Options |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "vue/v-on-handler-style": ["error", |
| 49 | + ["method", "inline-function"], // ["method", "inline-function"] | ["method", "inline"] | "inline-function" | "inline" |
| 50 | + { |
| 51 | + "ignoreIncludesComment": false |
| 52 | + } |
| 53 | + ] |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +- First option ... Specifies the name of an allowed style. Default is `["method", "inline-function"]`. |
| 58 | + - `["method", "inline-function"]` ... Allow handlers by method binding. e.g. `v-on:click="handler"`. Allow inline functions where method handlers cannot be used. e.g. `v-on:click="() => handler(listItem)"`. |
| 59 | + - `["method", "inline"]` ... Allow handlers by method binding. e.g. `v-on:click="handler"`. Allow inline handlers where method handlers cannot be used. e.g. `v-on:click="handler(listItem)"`. |
| 60 | + - `"inline-function"` ... Allow inline functions. e.g. `v-on:click="() => handler()"` |
| 61 | + - `"inline"` ... Allow inline handlers. e.g. `v-on:click="handler()"` |
| 62 | +- Second option |
| 63 | + - `ignoreIncludesComment` ... If `true`, do not report inline handlers or inline functions containing comments, even if the preferred style is `"method"`. Default is `false`. |
| 64 | + |
| 65 | +### `["method", "inline-function"]` (Default) |
| 66 | + |
| 67 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline-function']]}"> |
| 68 | + |
| 69 | +```vue |
| 70 | +<template> |
| 71 | + <!-- ✓ GOOD --> |
| 72 | + <button v-on:click="handler" /> |
| 73 | + <template v-for="e in list"> |
| 74 | + <button v-on:click="e" /> |
| 75 | + <button v-on:click="() => handler(e)" /> |
| 76 | + </template> |
| 77 | +
|
| 78 | + <!-- ✗ BAD --> |
| 79 | + <button v-on:click="handler()" /> |
| 80 | + <button v-on:click="count++" /> |
| 81 | + <button v-on:click="() => handler()" /> |
| 82 | + <button v-on:click="() => count++" /> |
| 83 | + <button v-on:click="(a, b) => handler(a, b)" /> |
| 84 | + <template v-for="e in list"> |
| 85 | + <button v-on:click="e()" /> |
| 86 | + <button v-on:click="() => e()" /> |
| 87 | + <button v-on:click="handler(e)" /> |
| 88 | + </template> |
| 89 | +</template> |
| 90 | +``` |
| 91 | + |
| 92 | +</eslint-code-block> |
| 93 | + |
| 94 | +### `["method", "inline"]` |
| 95 | + |
| 96 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline']]}"> |
| 97 | + |
| 98 | +```vue |
| 99 | +<template> |
| 100 | + <!-- ✓ GOOD --> |
| 101 | + <button v-on:click="handler" /> |
| 102 | + <template v-for="e in list"> |
| 103 | + <button v-on:click="e" /> |
| 104 | + <button v-on:click="handler(e)" /> |
| 105 | + </template> |
| 106 | +
|
| 107 | + <!-- ✗ BAD --> |
| 108 | + <button v-on:click="handler()" /> |
| 109 | + <button v-on:click="count++" /> |
| 110 | + <button v-on:click="() => handler()" /> |
| 111 | + <button v-on:click="() => count++" /> |
| 112 | + <button v-on:click="(a, b) => handler(a, b)" /> |
| 113 | + <template v-for="e in list"> |
| 114 | + <button v-on:click="e()" /> |
| 115 | + <button v-on:click="() => e()" /> |
| 116 | + <button v-on:click="() => handler(e)" /> |
| 117 | + </template> |
| 118 | +</template> |
| 119 | +``` |
| 120 | + |
| 121 | +</eslint-code-block> |
| 122 | + |
| 123 | +### `["inline-function"]` |
| 124 | + |
| 125 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['inline-function']]}"> |
| 126 | + |
| 127 | +```vue |
| 128 | +<template> |
| 129 | + <!-- ✓ GOOD --> |
| 130 | + <button v-on:click="() => handler()" /> |
| 131 | + <button v-on:click="() => count++" /> |
| 132 | +
|
| 133 | + <!-- ✗ BAD --> |
| 134 | + <button v-on:click="handler" /> |
| 135 | + <button v-on:click="handler()" /> |
| 136 | + <button v-on:click="handler($event)" /> |
| 137 | + <button v-on:click="count++" /> |
| 138 | +</template> |
| 139 | +``` |
| 140 | + |
| 141 | +</eslint-code-block> |
| 142 | + |
| 143 | +### `["inline"]` |
| 144 | + |
| 145 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['inline']]}"> |
| 146 | + |
| 147 | +```vue |
| 148 | +<template> |
| 149 | + <!-- ✓ GOOD --> |
| 150 | + <button v-on:click="handler()" /> |
| 151 | + <button v-on:click="handler($event)" /> |
| 152 | + <button v-on:click="count++" /> |
| 153 | +
|
| 154 | + <!-- ✗ BAD --> |
| 155 | + <button v-on:click="handler" /> |
| 156 | + <button v-on:click="() => handler()" /> |
| 157 | + <button v-on:click="(foo) => handler(foo)" /> |
| 158 | + <button v-on:click="(foo, bar) => handler(foo, bar)" /> |
| 159 | + <button v-on:click="() => count++" /> |
| 160 | +</template> |
| 161 | +``` |
| 162 | + |
| 163 | +</eslint-code-block> |
| 164 | + |
| 165 | +### `["method", "inline-function"], { "ignoreIncludesComment": true }` |
| 166 | + |
| 167 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline-function'], {ignoreIncludesComment: true}]}"> |
| 168 | + |
| 169 | +```vue |
| 170 | +<template> |
| 171 | + <!-- ✓ GOOD --> |
| 172 | + <button v-on:click="handler" /> |
| 173 | + <button v-on:click="() => handler() /* comment */" /> |
| 174 | +
|
| 175 | + <!-- ✗ BAD --> |
| 176 | + <button v-on:click="handler()" /> |
| 177 | + <button v-on:click="() => handler()" /> |
| 178 | + <button v-on:click="handler() /* comment */" /> |
| 179 | +</template> |
| 180 | +``` |
| 181 | + |
| 182 | +</eslint-code-block> |
| 183 | + |
| 184 | +### `["method", "inline"], { "ignoreIncludesComment": true }` |
| 185 | + |
| 186 | +<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline'], {ignoreIncludesComment: true}]}"> |
| 187 | + |
| 188 | +```vue |
| 189 | +<template> |
| 190 | + <!-- ✓ GOOD --> |
| 191 | + <button v-on:click="handler" /> |
| 192 | + <button v-on:click="handler() /* comment */" /> |
| 193 | +
|
| 194 | + <!-- ✗ BAD --> |
| 195 | + <button v-on:click="handler()" /> |
| 196 | + <button v-on:click="() => handler()" /> |
| 197 | + <button v-on:click="() => handler() /* comment */" /> |
| 198 | +</template> |
| 199 | +``` |
| 200 | + |
| 201 | +</eslint-code-block> |
| 202 | + |
| 203 | +## :couple: Related Rules |
| 204 | + |
| 205 | +- [vue/v-on-style](./v-on-style.md) |
| 206 | +- [vue/v-on-event-hyphenation](./v-on-event-hyphenation.md) |
| 207 | + |
| 208 | +## :books: Further Reading |
| 209 | + |
| 210 | +- [Guide - Inline Handlers] |
| 211 | +- [Guide - Method Handlers] |
| 212 | + |
| 213 | +[Guide - Inline Handlers]: https://vuejs.org/guide/essentials/event-handling.html#inline-handlers |
| 214 | +[Guide - Method Handlers]: https://vuejs.org/guide/essentials/event-handling.html#method-handlers |
| 215 | + |
| 216 | +## :mag: Implementation |
| 217 | + |
| 218 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/v-on-handler-style.js) |
| 219 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/v-on-handler-style.js) |
0 commit comments