|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/no-restricted-custom-event |
| 5 | +description: disallow specific custom event |
| 6 | +--- |
| 7 | +# vue/no-restricted-custom-event |
| 8 | +> disallow specific custom event |
| 9 | +
|
| 10 | +## :book: Rule Details |
| 11 | + |
| 12 | +This rule allows you to specify custom event that you don't want to use in your application. |
| 13 | + |
| 14 | +## :wrench: Options |
| 15 | + |
| 16 | +This rule takes a list of strings, where each string is a custom event name or pattern to be restricted: |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "vue/no-restricted-custom-event": ["error", "input", "/^forbidden/"] |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +<eslint-code-block :rules="{'vue/no-restricted-custom-event': ['error', 'input', '/^forbidden/']}"> |
| 25 | + |
| 26 | +```vue |
| 27 | +<template> |
| 28 | + <!-- ✗ BAD --> |
| 29 | + <input @input="$emit('input', $event.target.value)"> |
| 30 | + <!-- ✗ GOOD --> |
| 31 | + <input @input="$emit('update:value', $event.target.value)"> |
| 32 | +</template> |
| 33 | +<script> |
| 34 | +export default { |
| 35 | + methods: { |
| 36 | + handleChangeValue(newValue) { |
| 37 | + /* ✗ BAD */ |
| 38 | + this.$emit('input', newValue) |
| 39 | + this.$emit('forbiddenEvent') |
| 40 | +
|
| 41 | + /* ✓ GOOD */ |
| 42 | + this.$emit('foo') |
| 43 | + this.$emit('bar') |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +</script> |
| 48 | +``` |
| 49 | + |
| 50 | +</eslint-code-block> |
| 51 | + |
| 52 | + |
| 53 | +Alternatively, the rule also accepts objects. |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "vue/no-restricted-custom-event": ["error", |
| 58 | + { |
| 59 | + "event": "input", |
| 60 | + "message": "If you intend a prop for v-model, it should be 'update:modelValue' in Vue 3.", |
| 61 | + "suggest": "update:modelValue" |
| 62 | + }, |
| 63 | + ] |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The following properties can be specified for the object. |
| 68 | + |
| 69 | +- `event` ... Specify the event name or pattern. |
| 70 | +- `message` ... Specify an optional custom message. |
| 71 | +- `suggest` ... Specify an optional name to suggest changes. |
| 72 | + |
| 73 | +<eslint-code-block :rules="{'vue/no-restricted-custom-event': ['error', { event: 'input', message: 'If you intend a prop for v-model, it should be \'update:modelValue\' in Vue 3.', suggest: 'update:modelValue'}]}"> |
| 74 | + |
| 75 | +```vue |
| 76 | +<template> |
| 77 | + <!-- ✗ BAD --> |
| 78 | + <input @input="$emit('input', $event.target.value)"> |
| 79 | +</template> |
| 80 | +<script> |
| 81 | +export default { |
| 82 | + methods: { |
| 83 | + handleChangeValue(newValue) { |
| 84 | + /* ✗ BAD */ |
| 85 | + this.$emit('input', newValue) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +</script> |
| 90 | +``` |
| 91 | + |
| 92 | +</eslint-code-block> |
| 93 | + |
| 94 | +## :mag: Implementation |
| 95 | + |
| 96 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-custom-event.js) |
| 97 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-custom-event.js) |
0 commit comments