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

Detailed explanation of how to write mysql not equal to null and equal to null

1. Table structure 2. Table data 3. The query tea...

Vue login function implementation

Table of contents Written in front Login Overview...

Implementation steps for installing RocketMQ in docker

Table of contents 1. Retrieve the image 2. Create...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

MySQL full-text search usage examples

Table of contents 1. Environmental Preparation 2....

Network management and network isolation implementation of Docker containers

1. Docker network management 1. Docker container ...

Getting Started Tutorial on GDB in Linux

Preface gdb is a very useful debugging tool under...

Is it necessary to give alt attribute to img image tag?

Do you add an alt attribute to the img image tag? ...

JS implementation of carousel example

This article shares the specific code of JS to im...

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...

Web developers are concerned about the coexistence of IE7 and IE8

I installed IE8 today. When I went to the Microso...

How to write the parent and child directories of HTML relative paths

How to indicate the parent directory ../ represent...

How to prohibit vsftpd users from logging in through ssh

Preface vsftp is an easy-to-use and secure ftp se...

Problems with join queries and subqueries in MySQL

Table of contents Basic syntax for multi-table jo...