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 explanation of mysql.user user table in Mysql

MySQL is a multi-user managed database that can a...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Introduction to the steps of deploying redis in docker container

Table of contents 1 redis configuration file 2 Do...

MySQL uses binlog logs to implement data recovery

MySQL binlog is a very important log in MySQL log...

JavaScript Closures Explained

Table of contents 1. What is a closure? 1.2 Memoi...

How does MySQL achieve master-slave synchronization?

Master-slave synchronization, also called master-...

How to set mysql permissions using phpmyadmin

Table of contents Step 1: Log in as root user. St...

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

WeChat applet implements a simple calculator

A simple calculator written in WeChat applet for ...

js implements random roll call

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

Vue directives v-html and v-text

Table of contents 1. v-text text rendering instru...

30 minutes to give you a comprehensive understanding of React Hooks

Table of contents Overview 1. useState 1.1 Three ...