JS.VUE.REQUIRE.EXPLICIT.EMITS

Require 'emits' option with name triggered by '$emit()'

vue/require-explicit-emits

require emits option with name triggered by $emit()

  • Some problems reported by this rule are manually fixable by editor suggestions (https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

Rule Details

This rule reports event triggers not declared with the emits option. (The emits option is a new in Vue.js 3.0.0+)

Explicit emits declaration serves as self-documenting code. This can be useful for other developers to instantly understand what events the component is supposed to emit. Also, with attribute fallthrough changes in Vue.js 3.0.0+, v-on listeners on components will fallthrough as native listeners by default. Declare it as a component-only event in emits to avoid unnecessary registration of native listeners.

{'vue/require-explicit-emits': ['error']}

Copy
<template>
  <!-- GOOD -->
  <div @click="$emit('good')"/>
  <!-- BAD -->
  <div @click="$emit('bad')"/>
</template>
<script>
export default {
  emits: ['good']
}
</script>

{'vue/require-explicit-emits': ['error']}

Copy
<script>
export default {
  emits: ['good'],
  methods: {
    foo () {
      // GOOD
      this.$emit('good')
      // BAD
      this.$emit('bad')
    }
  }
}
</script>

{'vue/require-explicit-emits': ['error']}

Copy
<script>
export default {
  emits: ['good'],
  setup (props, context) {
    // GOOD
    context.emit('good')
    // BAD
    context.emit('bad')
  }
}
</script>

Options

Copy
{
  "vue/require-explicit-emits": ["error", {
    "allowProps": false
  }]
}
  • "allowProps" ... If true, allow event names defined in props. default false

"allowProps": true

{'vue/require-explicit-emits': ['error', {allowProps: true}]}

Copy
<script>
export default {
  props: ['onGood', 'bad'],
  methods: {
    foo () {
      // GOOD
      this.$emit('good')
      // BAD
      this.$emit('bad')
    }
  }
}
</script>

Further Reading

  • Guide - Custom Events / Defining Custom Events (https://v3.vuejs.org/guide/component-custom-events.html#defining-custom-events)
  • Vue RFCs - 0030-emits-option (https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md)

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/