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

Implementation of MySQL scheduled backup script under Windows

On a Windows server, if you want to back up datab...

An article to understand what is MySQL Index Pushdown (ICP)

Table of contents 1. Introduction 2. Principle II...

MySQL multi-table join introductory tutorial

Connections can be used to query, update, and est...

Detailed discussion of InnoDB locks (record, gap, Next-Key lock)

Record lock locks a single index record. Record l...

Detailed explanation of VUE's data proxy and events

Table of contents Review of Object.defineProperty...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

JavaScript to achieve simple provincial and municipal linkage

This article shares the specific code for JavaScr...

js to achieve image fade-in and fade-out effect

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

Design Theory: A Method to Understand People's Hearts

<br />Once, Foyin and Mr. Dongpo were chatti...

Alibaba Cloud Ubuntu 16.04 builds IPSec service

Introduction to IPSec IPSec (Internet Protocol Se...

Detailed tutorial on installing mysql8.0.22 on Alibaba Cloud centos7

1. Download the MySQL installation package First ...

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...