Detailed explanation of various methods of Vue component communication

Detailed explanation of various methods of Vue component communication

When it comes to component communication, there are probably five types that are familiar to everyone:

1. From father to son

The parent component passes values ​​to the child component through custom attributes, and the child component receives them using props

2. From son to father

The parent component customizes the event on the child component tag, and the child component triggers the custom event through $emit and can pass parameters

3. Non-parent-child component value transfer

eventBus event bus:

Information can be published and subscribed through eventBus (creating an event bus that everyone can access)

insert image description here

4. vuex

Vuex is a state management mode developed specifically for Vue.js applications. It uses a centralized storage manager to manage the status of all components of the program and solve multi-component communication. However, it is mostly used in medium and large projects, and it will appear bloated in small projects.

5. Refs

By adding ref and ·$refs·, you can also easily get the child component and access the properties and methods of the child component. Regarding refs, I have also written a separate article with detailed usage, the usage of refs

In addition to the above 5, there are 5 less common component communication methods:

6. $children

In the parent component, $children returns a component collection. If you know the order of the child components, you can also use the subscript operation

insert image description here

7. $parent

In the child component this.$parent points to the parent component. The child component cannot directly modify the data in the parent component. You can use this.$parent to let the parent component modify it itself.

For example:

this.$parent.xxx=200

this.$parent.fn()

8. provide & inject

Appear in pairs : provide and inject appear in pairs

Function : Used by parent components to pass data to descendant components

Directions:

  • Provide in the parent component, returns the data to be passed to the subordinate
  • inject injects data into descendant components that need to use this data. (regardless of how deep the component hierarchy is)

insert image description here

9. $attrs

Used by parent components to pass data to grandchild components

10. $listeners

Used by grandchild components to pass data to parent components

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:
  • Detailed explanation of custom events of Vue components
  • Detailed explanation of value transfer between parent and child components in Vue3
  • Vue components dynamic components detailed explanation
  • Detailed explanation of the use of Vue image drag and drop zoom component
  • Detailed explanation of the use of router-view components in Vue
  • Detailed explanation of Vue components

<<:  Docker5 full-featured harbor warehouse construction process

>>:  Introduction to the use of data custom attributes in HTML and plug-in applications

Recommend

Using CSS3's 3D effects to create a cube

Learning to use CSS3's 3D effects to create a...

What is web design

<br />Original article: http://www.alistapar...

Docker installs Redis and introduces the visual client for operation

1 Introduction Redis is a high-performance NoSQL ...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

What knowledge systems do web designers need?

Product designers face complex and large manufactu...

Summary of MySQL logical backup and recovery testing

Table of contents 1. What kind of backup is a dat...

Detailed explanation of setting resource cache in nginx

I have always wanted to learn about caching. Afte...

Summary of ways to implement single sign-on in Vue

The project has been suspended recently, and the ...

Vue.js implements tab switching and color change operation explanation

When implementing this function, the method I bor...

Use of Linux passwd command

1. Command Introduction The passwd command is use...

Linux View File System Type Example Method

How to check the file system type of a partition ...