Detailed explanation of data sharing between Vue components

Detailed explanation of data sharing between Vue components

1. In project development, the most common relationships between components are divided into the following two types:

1. Father-son relationship

2. Brotherly Relationship

1.1 Data sharing between parent and child components

Data sharing between parent and child components is divided into:

1. Parent->Child Shared Data

Subcomponents:

insert image description here

Parent component:

insert image description here

2. Child->Parent Shared Data

Child components share data with parent components using custom events. The sample code is as follows

Subcomponents:

insert image description here

Parent component:

insert image description here

The page displays the results:

insert image description here

1.2 Data sharing between sibling components

In vue2.x, the solution for sharing data between sibling components is EventBus

EventBus usage:

  • Create the eventBus.js module and share a Vue instance object
  • On the data sender, call bus.$emit('event name', data to be sent) method to trigger a custom event
  • On the data receiving side, call bus.$on('event name', event handling function) method to register a custom event

Example:

1. Create the eventBus.js module and share a Vue instance object

insert image description here

2. On the data sender, call bus.$emit('event name', data to be sent) method to trigger a custom event

insert image description here

3. On the data receiver side, call bus.$on('event name', event processing function) method to register a custom event

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Let's learn about Vue's life cycle
  • Detailed explanation of Vue3 life cycle hook function
  • A brief discussion on the life cycle of Vue
  • Vue local component data sharing Vue.observable() usage
  • Vue implements data sharing and modification operations between two components
  • Detailed explanation of Vue life cycle and data sharing

<<:  The process of installing Docker on Windows Server 2016 and the problems encountered

>>:  Html Select option How to make the default selection

Recommend

MySQL restores data through binlog

Table of contents mysql log files binlog Binlog l...

Summary of 50+ Utility Functions in JavaScript

JavaScript can do a lot of great things. This art...

The meaning of the 5 types of spaces in HTML

HTML provides five space entities with different ...

JavaScript to implement limited time flash sale function

This article shares the specific code of JavaScri...

Solution to 1045 error when navicat connects to mysql

When connecting to the local database, navicat fo...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

Element-ui directly clicks on the cell in the table to edit

Table of contents Achieve results Implementation ...

Solve the problem of secure_file_priv null

Add secure_file_priv = ' '; then run cmd ...

In-depth discussion on auto-increment primary keys in MySQL

Table of contents Features Preservation strategy ...

Nginx domain name SSL certificate configuration (website http upgraded to https)

Preface HTTP and HTTPS In our daily life, common ...

Learn more about using regular expressions in JavaScript

Table of contents 1. What is a regular expression...

Mysql sets boolean type operations

Mysql sets boolean type 1. Tinyint type We create...

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation fail...

Example of how to quickly delete a 2T table in mysql in Innodb

Preface This article mainly introduces the releva...