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

Vue realizes the palace grid rotation lottery

Vue implements the palace grid rotation lottery (...

An article to give you a deep understanding of Mysql triggers

Table of contents 1. When inserting or modifying ...

How to build a complete samba server in Linux (centos version)

Preface smb is the name of a protocol that can be...

Complete steps to implement face recognition login in Ubuntu

1. Install Howdy: howdy project address sudo add-...

Detailed explanation of Vue's keyboard events

Table of contents Common key aliases Key without ...

Detailed explanation of MySQL batch SQL insert performance optimization

For some systems with large amounts of data, the ...

Canonical enables Linux desktop apps with Flutter (recommended)

Google's goal with Flutter has always been to...

Explanation of the usage scenarios of sql and various nosql databases

SQL is the main trunk. Why do I understand it thi...

Docker+gitlab+jenkins builds automated deployment from scratch

Table of contents Preface: 1. Install Docker 2. I...

Record of the actual process of packaging and deployment of Vue project

Table of contents Preface 1. Preparation - Server...

How to view the execution time of SQL statements in MySQL

Table of contents 1. Initial SQL Preparation 2. M...

Detailed process of upgrading glibc dynamic library in centos 6.9

glibc is the libc library released by gnu, that i...