Vue3 emits Property | Rookie Tutorial
Rookie Tutorial --
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Vue3 Tutorial
- Vue3 Tutorial
- Vue3 Introduction
- Vue3 Installation
- VSCode Vue
- Vue3 Build
- Vue3 Create Project
- Vue3 Project Introduction
- Vite Tutorial
- Vue3 Directory Structure
- Vue3 Getting Started
- Vue3 Basic Syntax
- Vue3 Declarative Rendering
- Vue3 Directives
- Vue3 Template Syntax
- Vue3 Conditional Statements
- Vue3 Loop Statements
- Vue3 Components
- Vue3 Computed Properties
- Vue3 Watch Properties
- Vue3 Style Binding
- Vue3 Event Handling
- Vue3 Forms
- Vue3 Custom Directives
- Vue3 Routing
- Vue3 Mixins
- Vue3 Ajax(axios)
- Vue3 Composition API
- Vue3 Single File Components
- Vue3 Pinia
Reference Manual
- Vue3 Built-in Attributes
- Vue3 Built-in Components
- Vue Component Instance
- Vue3 Built-in Directives
- Vue Instance Options
- Vue3 Lifecycle Hooks
- Vue3 Tailwind CSS
- Vue3 Global API
- Vue3 Options API
- Vue3 Quiz
Practical Case 1
- Vue3 Task Management System
- Environment Setup
- Core Business Development
- Component Splitting
- State Management and Data Persistence
- Routing System
- Logic Reuse and Performance Optimization
- Project Deployment
Practical Case 2
- Vue3 Blog Project
- Template Syntax Rendering
- Reactive Data
- Split Components
- Routing Navigation
- Lifecycle and Data Requests
- watch and Search Functionality
- Composable β Encapsulating Reusable Logic
- Pinia β Global State Management
- Build and Deploy β Deploy to Vercel
Deep Dive
Software
Network Services
Web Service
Programming Languages
Computer Science
Network Design and Development
Scripts
Development Tools
Programming
Scripting Languages
Vue3 emits Property
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:
- Clear Event Declaration: Through the
emits property, developers can clearly know which events the component will trigger.
- Code Readability: Viewing the
emits property in a component allows quick understanding of the component's event interactions.
- 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
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.emits property, developers can better manage component events and improve code readability and maintainability.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:emits property, developers can clearly know which events the component will trigger.emits property in a component allows quick understanding of the component's event interactions.emits, Vue will issue a warning in development mode to help developers discover potential issues.emits can be an array listing all custom event names.<script>
export default{
emits:['update:modelValue','submit'],
methods:{
handleClick(){
this.$emit('submit','Form submitted!');
}
}
}
</script>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.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.<script>
export default{
e
YouTip