The difference and usage of Vue2 and Vue3 brother component communication bus

The difference and usage of Vue2 and Vue3 brother component communication bus

vue2.x

  1. Vue.prototype.$bus = new Vue()
  2. Listening: this.$bus.$on('method name', (parameters) => {}), it can be accumulated
  3. Trigger: this.$bus.$emit('method name', actual parameter value)
  4. Destroy: this.$bus.$off('method name'), whoever listens will destroy it
  5. Note: Since monitoring can be accumulated, a fourth step of destruction is required

vue3.x

tiny-emitter plugin usage

Install the plugin npm i tiny-emitter

Import and use

import emitter from 'tiny-emitter/instance'

import { onMounted } from 'vue'

setup(){
       onMounted(()=>{
      		Monitoring: emitter.on('Monitoring method name', (parameters) => {callback function})
      		Trigger: emitter.emit('listening method name', parameters)
      		Destruction: emitter.off('listening method name', parameter)
       }) 
}
	    

Mitt plugin usage

1. Install npm install

2. Import and use

import mitt from 'mitt'

Monitoring: bus.on('Monitoring method name', (parameters) => {callback function})

Trigger: bus.emit('listening method name', parameter)

Destruction: bus.off('listening method name', parameter)

The above is the difference and usage of the communication bus between Vue2 and Vue3 brother components. For more information about the usage of the communication bus between Vue2 and Vue3 brother components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue.js parent-child component communication development example
  • Parent-child component communication in Vue and using sync to synchronize parent-child component data
  • Development of todolist component function in parent-child component communication in Vue
  • 12 types of component communications in Vue2
  • Summary of 10 component communication methods in Vue3

<<:  Example method to view the IP address connected to MySQL

>>:  Analyze the working principle of Tomcat

Recommend

Answers to several high-frequency MySQL interview questions

Preface: In interviews for various technical posi...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...

Six important selectors in CSS (remember them in three seconds)

From: https://blog.csdn.net/qq_44761243/article/d...

CUDA8.0 and CUDA9.0 coexist under Ubuntu16.04

Preface Some of the earlier codes on Github may r...

Detailed steps to install Nginx on Linux

1. Nginx installation steps 1.1 Official website ...

js to achieve simple image drag effect

This article shares the specific code of js to ac...

CSS Houdini achieves dynamic wave effect

CSS Houdini is known as the most exciting innovat...

Vue's new partner TypeScript quick start practice record

Table of contents 1. Build using the official sca...

Summary of CSS gradient effects (linear-gradient and radial-gradient)

Linear-gradient background-image: linear-gradient...

A brief analysis of the basic implementation of Vue detection data changes

Table of contents 1. Object change detection 2. Q...

Solution to MySQL 8.0 cannot start 3534

MySQL 8.0 service cannot be started Recently enco...

An example of installing MySQL on Linux and configuring external network access

Configuration steps 1. Check whether DNS is confi...

80 lines of code to write a Webpack plugin and publish it to npm

1. Introduction I have been studying the principl...

Installation and deployment of MySQL Router

Table of contents 01 Introduction to MySQL Router...

Implementing image fragmentation loading function based on HTML code

Today we will implement a fragmented image loadin...