Detailed explanation of the use of bus in Vue

Detailed explanation of the use of bus in Vue

Vue bus mechanism (bus)

In addition to using vuex, communication between non-parent-child components in vue can also be done through the bus, and the two are applicable to different scenarios.

The bus is suitable for small projects and projects where data is used by fewer components. It is not suitable for medium and large projects where data is used among many components. Bus is actually a publish-subscribe model. It uses Vue's custom event mechanism to publish an event through $emit at the triggering location, and listens to the event through $on on the page that needs to be listened to.

Vuex is suitable for medium and large projects and situations where data is shared among multiple components.

Use of component communication bus

Create bus.js under the utils file

// utils - bus.js
import Vue from 'vue'
const bus = new Vue()
export default bus

1. Passing Values

Send Message

import bus from '@/utils/bus'

The first parameter is the flag variable, and the second parameter is the communication value.

us.$emit('message', 'hello');

Receiving information

import bus from '@/utils/bus'

The first parameter is the flag variable, and the e in the second parameter is the communication value.

bus.$on('message', (e) => {
 console.log(e)
})

2. Calling method

One component (A) calls a method of another component (B)

Methods of component B

import bus from '@/utils/bus'
mounted () { 
 bus.$on('testA', this.testA) 
},
testA () {
 console.log('Called by component A')
}

A component call

import bus from '@/utils/bus'
mounted () {
 bus.$emit('testA')
}

This is the end of this article about the use of bus in Vue. For more relevant content on the use of vue bus, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue uses eventBus to realize communication between peer components
  • A very detailed summary of communication between Vue components
  • Sample code for implementing sibling component communication using eventBus in vue2.0s
  • Eight ways of component communication in Vue (worth collecting!)
  • The difference and usage of Vue2 and Vue3 brother component communication bus

<<:  Introduction to version management tool Rational ClearCase

>>:  Install docker offline by downloading rpm and related dependencies using yum

Recommend

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...

4 principles for clean and beautiful web design

This article will discuss these 4 principles as t...

Detailed analysis of javascript data proxy and events

Table of contents Data Brokers and Events Review ...

Detailed explanation of the construction and use of docker private warehouse

1. Download the repository image docker pull regi...

Detailed explanation of several methods of installing software in Linux

1. RPM package installation steps: 1. Find the co...

Practical MySQL + PostgreSQL batch insert update insertOrUpdate

Table of contents 1. Baidu Encyclopedia 1. MySQL ...

Detailed tutorial on deploying SpringBoot + Vue project to Linux server

Preface Let me share with you how I deployed a Sp...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...

Complete steps for uninstalling MySQL database

The process of completely uninstalling the MySQL ...

VUE+Express+MongoDB front-end and back-end separation to realize a note wall

I plan to realize a series of sticky note walls. ...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

Analysis of log files in the tomcat logs directory (summary)

Each time tomcat is started, the following log fi...

Installing Win10 system on VMware workstation 14 pro

This article introduces how to install the system...