Validating custom events and emitting events using defineEmits in Vue 3 Composition API

Posted by


In Vue 3, custom events validation has become much simpler and more powerful with the introduction of the defineEmits function in the Vue Composition API. This function allows you to define the types of events that a component can emit and automatically validate them, ensuring that only valid events are emitted.

To get started with custom events validation using defineEmits, first make sure you have Vue 3 installed in your project. If you haven’t already done so, you can install Vue 3 with the following command:

npm install vue@next

Once you have Vue 3 installed, you can start creating a new Vue component using the Composition API. Here’s an example of a basic Vue component that emits a custom event:

<template>
  <button @click="emitCustomEvent">Emit Custom Event</button>
</template>

<script>
import { defineComponent } from 'vue'

export default defineComponent({
  setup() {
    const emitCustomEvent = () => {
      // Emit a custom event
      console.log('Custom event emitted')
      emit('custom-event', 'Custom event data')
    }

    return {
      emitCustomEvent
    }
  }
})
</script>

In this example, the component emits a custom event called ‘custom-event’ when the button is clicked. However, without custom events validation, the event could be emitted with any data, even if it is not valid. This is where defineEmits comes in.

To use defineEmits for custom events validation, you need to define the types of events that the component can emit in the setup function. Here’s an updated version of the above example with custom events validation using defineEmits:

<template>
  <button @click="emitCustomEvent">Emit Custom Event</button>
</template>

<script>
import { defineComponent, defineEmits } from 'vue'

const customEvents = ['custom-event']

export default defineComponent({
  setup(_, { emit }) {
    const emitCustomEvent = () => {
      // Emit a custom event
      console.log('Custom event emitted')
      emit('custom-event', 'Custom event data')
    }

    defineEmits(customEvents)

    return {
      emitCustomEvent
    }
  }
})
</script>

In this updated example, we have defined an array called customEvents that contains the names of all the custom events that the component can emit. We then use defineEmits to define these custom events and validate them.

Now, when the component emits an event using emit, Vue will automatically validate the event against the defined custom events, ensuring that only valid events are emitted. If an invalid event is emitted, Vue will throw a warning in the console.

Overall, custom events validation using defineEmits in Vue 3 provides a powerful way to ensure that the events emitted by a component are valid and consistent. This can help improve code quality and prevent unexpected behavior in your Vue applications.

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@tony-gx7rt
6 hours ago

could make aplylist on vuejs3 script setup and apollo client graphql

@alperaslan.
6 hours ago

Great explanation thank you 🙂

@pinyin1
6 hours ago

May God Almighty give you more grace

3
0
Would love your thoughts, please comment.x
()
x