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

2 methods and precautions for adding scripts in HTML

How to add <script> script in HTML: 1. You c...

mysql 5.7.18 winx64 free installation configuration method

1. Download 2. Decompression 3. Add the path envi...

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, ...

CSS animation combined with SVG to create energy flow effect

The final effect is as follows: The animation is ...

How to deploy code-server using docker

Pull the image # docker pull codercom/code-server...

Vue implements a movable floating button

This article example shares the specific code of ...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

Some parameter descriptions of text input boxes in web design

<br />In general guestbooks, forums and othe...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

How to use type enhancement without typingscript

Preface Due to the weak typing of JS, loose writi...

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...

Complete steps for deploying confluence with docker

Confluence is paid, but it can be cracked for use...