Simple use of Vue bus

Simple use of Vue bus

Simple use of Vue bus

Scenario description:

Component A includes components B and C, and component B includes component D. What if component D wants to trigger the method of component C in component A?

Of course there are solutions, you can use vuex to manage the state, or you can use $emit to pass it, but I want to try the bus method;

as follows:

The emit of the bus is triggered in the D component, and then the on of the bus is used to trigger the method in the A component;

In component D

dataLoad(){
   console.log('Loading complete trigger event');
   this.$bus.$emit('itemDataLoad') 
   // this.$bus.$emit('event name', parameter) // The second one can be a parameter},

In component A

 mounted() {
    // Listen for data loading in item this.$bus.$on('itemDataLoad', () => {
      console.log('Data loading completed');
    })
    // this.$bus.$on('event name', callback function (parameters))
  },

Of course, in component A, events in component C can be triggered by this.$refs , etc.

Another step is that $bus does not exist by default. Try printing this.$bus為undefined .

Don't worry, add $bus in main.js;

// bus bus vue instance Vue.prototype.$bus = new Vue()

Of course, the bus can be removed during the life cycle;

this.$bus.$off();

Record encapsulated anti-shake function

// debounce function: function (fun, delay) {
    let timer = null
    // Receive the value of the parameter passed in when calling the function... args can be multiple return function (...args) {
      if (tiemr) return
      timer = setTimeout(() => {
        fun.apply(this, args)
      }, delay);
    }
  }

const refresh = debounce(xxx, 500)

refresh('parameter 1', 'parameter 2', 'parameter 3')

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

You may also be interested in:
  • How much do you know about custom events in Vue components
  • Vue component learning scoped detailed explanation
  • Vue publish-subscribe model implementation process diagram
  • Detailed explanation of the practical application of the publish-subscribe model in Vue
  • Detailed explanation of Vue bus mechanism (bus) knowledge points
  • Detailed explanation of event bus and message publishing and subscription of Vue components

<<:  How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04

>>:  MySql COALESCE function usage code example

Recommend

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

A complete record of a Mysql deadlock troubleshooting process

Preface The database deadlocks I encountered befo...

MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci

Reference: MySQL character set summary utf8mb4 ha...

Practical basic Linux sed command example code

The Linux stream editor is a useful way to run sc...

React sample code to implement login form

As a Vue user, it's time to expand React. Fro...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

Solutions to MySQL batch insert and unique index problems

MySQL batch insert problem When developing a proj...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

MySQL scheduled database backup operation example

This article describes the example of MySQL sched...

JavaScript to implement simple tab bar switching content bar

This article shares the specific code of JavaScri...

CSS implements five common 2D transformations

2D transformations in CSS allow us to perform som...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

Two problems encountered when deploying rabbitmq with Docker

1. Background The following two problems are enco...

Graphical introduction to the difference between := and = in MySQL

The difference between := and = = Only when setti...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...