Search code examples
vue.jsvuejs3vue-composition-apivue-script-setup

Vue Composition API: Defining emits


When defining custom events Vue encourages us to define emitted events on the component via the emits option:

app.component('custom-form', {
  emits: ['inFocus', 'submit']
})

Using Vue 3's composition API, when a standalone composition function emits custom events, is it possible to define these in the composition function?


Solution

  • If you are using script setup you can use defineEmits which is a compiler macros and you don't have to import it:

    <script setup>
    const emit = defineEmits(['inFocus', 'submit'])
    
    emit('inFocus')
    </script>
    

    You can also use an object syntax, which allows performing events validation:

    <script setup>
    const emit = defineEmits({
      // No validation
      inFocus: null,
    
      // Validate submit event
      submit: ({ email, password }) => {
        if (email && password) return true
        else return false
      }
    })
    
    function submitForm(email, password) {
      emit('submit', { email, password })
    }
    </script>
    

    Note: the submit event will be emitted regardless of validation but if the validation doesn't pass, you will get a Vue warning:

    [Vue warn]: Invalid event arguments: event validation failed for event "submit".

    See it live


    Typing with TS:

    <script setup lang="ts">
    const emit = defineEmits<{
      (e: 'change', id: number): void
      (e: 'update', value: string): void
    }>()
    </script>
    

    The above code snippets are taken from Vue.js Emitting and Listening to Events