YouTip LogoYouTip

Vue3 Api Emits

Vue3 emits Property | Rookie Tutorial

Rookie Tutorial --

Vue3 Tutorial

Reference Manual

Practical Case 1

Practical Case 2

Vue3 Global API

Vue3 Quiz

Deep Dive

Software

Network Services

Web Service

Programming Languages

Computer Science

Network Design and Development

Scripts

Development Tools

Programming

Scripting Languages

Vue3 emits Property


Image 3: Vue3 Options API Vue3 Options API

In Vue3, the emits property is used to define the events that a component can trigger.

emits is an array or object used to explicitly declare which custom events the component will emit.

Through the emits property, developers can better manage component events and improve code readability and maintainability.


Why Use the emits Property?

In Vue2, parent components listen to child component events through v-on, while child components trigger events through the $emit method. Although this approach is simple and direct, as project scales grow, event management can become chaotic. Vue3 introduces the emits property for the following benefits:

  1. Clear Event Declaration: Through the emits property, developers can clearly know which events the component will trigger.
  2. Code Readability: Viewing the emits property in a component allows quick understanding of the component's event interactions.
  3. Type Checking and Warnings: If a triggered event is not declared in emits, Vue will issue a warning in development mode to help developers discover potential issues.

How to Use the emits Property?

1. Basic Usage

In a component, emits can be an array listing all custom event names.

Example

<script>

export default{
  emits:['update:modelValue','submit'],
  
  methods:{
    handleClick(){
      this.$emit('submit','Form submitted!');
    }
  }
}

</script>

In the example above, the emits array declares two events: update:modelValue and submit. When this.$emit('submit', 'Form submitted!') is called, the submit event is triggered and the data 'Form submitted!' is passed.


2. Object Usage

emits can also be an object where the keys are event names and the values are validation functions. The validation function is used to check whether the passed event parameters are valid.

Example

<script>

export default{
  e
← Vue3 Api TemplateVue3 Api Methods β†’