Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue3 emits Property

Vue3 emits Property


Vue3 Options API Vue3 Options API

In Vue 3,emits The property is used to define the events a component can emit.

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

Pass emits property, developers can better manage a component's events and improve code readability and maintainability.


Why Use the emits Property?

In Vue 2, the parent component listens to the child component's events via v-on , while the child component triggers events via the $emit method. Although this approach is simple and direct, as the project grows, event management can become chaotic. Vue 3 introduced emits property, which offers the following benefits:

  1. Clear Event Declaration: Through emits property, developers can clearly know which events the component will emit.
  2. Code Readability: By viewing the emits property in a component, you can quickly understand its event interactions.
  3. Type Checking and Warnings: If an emitted event is not declared in the emits , Vue will issue a warning in development mode, helping developers spot potential issues.

How to Use the emits Property?

1. Basic Usage

In a component,emits can be an array listing the names of all custom events.

Example

<

script

export

default

{

emits

:

[

'update:modelValue'

,

'submit'

]

,

  methods

:

{

    handleClick

(

)

{

     

this

.$emit

(

'submit'

,

'Form submitted!'

)

;

   

}

 

}

}

script

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


2. Object Syntax

emits can also be an object, where the key is the event name and the value is a validation function. The validation function checks whether the passed event arguments are valid.

Example

<

script

export

default

{

emits

:

{

    submit

:

(

payload

)

=>

{

     

if

(

typeof

payload

===

'string'

)

{

       

return

true

;

     

}

else

{

        console.

warn

(

'Submit event payload must be a string!'

)

;

       

return

false

;

     

}

   

}

 

}

,

  methods

:

{

    handleClick

(

)

{

     

this

.$emit

(

'submit'

,

'Form submitted!'

)

;

   

}

 

}

}

script

In this example,submit The validation function for the event checks whether payload is a string. If not, it outputs a warning message to the console.


3. Using with v-model

emits The property is often used together with v-model , especially when handling two-way binding in custom components.

Example

In this example, when the input value changes, the update:modelValue event is triggered and the new value is passed to the parent component.


Notes

  1. Event Name Casing: Event names in Vue are case-insensitive, but kebab-case (hyphenated) naming is recommended.
  2. Return Value of the Validation Function: If the validation function returns false, Vue will issue a warning in development mode, but the event will still be triggered.
  3. Default Events: Certain native events (such as click) do not need to be declared in the emits unless you need to validate their arguments.

Vue3 Options API Vue3 Options API