VUE event bus eventbus

First, introduction to eventbus

EventBus is also known as the event bus. In Bue, you can use EventBus as a communication bridge concept, just as all components share the same event center, you can register to send events or receive events to the center, so components can notify other components in parallel, but also too Convenience, if used inadvertently, it will cause a disaster that is difficult to maintain, so Vue provides a more complete Vuex as a state management center, raising the concept of notification to the shared state level, so we must be cautious when using eventbus.

Second, the problem needs to be solved

A problem has been encountered in the project. After the page jumps, the new page is not refreshed. After the first load, the page is loaded, and then the data returned and the page is not changed. The logic is that the page jump will take the parameters, the new page will display the content according to this parameter on the page, if not refreshed later, the data displayed on the page will not change.
So I need to re-call the method of loading the table data after the page jump, just the eventbus bus can meet my requirements, I initiate an event in the page jump method, in the new The page's mounted method (the method that needs to be executed when the page is loaded) accepts and responds to the event, recalling the method of table data loading, so that my problem can be solved.

Third, how to use

(1) Create a js file of EventBus

/ / Create a new js file named eventbus, add the following code in the file
import Vue from 'vue'
export default new Vue;

(two) global call

Import eventbus in main.js and mount it on the prototype of vue

import bus from './utils/eventBus'
Vue.prototype.bus = bus;

(3) Sending an event

Send an event where the event is triggered, for example, I add the code to send the event where the page jumps

//clazzCourseCode is the event name
this.bus.$emit("clazzCourseCode");

(4) Receiving events and responding

mounted() {
         / / Here write method call, the method in mounted () {} indicates that the method needs to be executed when the page is loaded.
         //bus bus response event--the event name needs to be the same
    this.bus.$on("clazzCourseCode", () => {
         / / Method of loading table data
      this.queryMember();
    });
  },

(5) Untieing the incident

 beforeDestroy() {
         / / Need to unbind the event before the component is destroyed. Otherwise there will be a problem with repeated trigger events
    this.bus.$off("clazzCourseCode");
  },

Intelligent Recommendation

Vue Transfer Event Bus Eventbus

There is a simple method between components in Vue. Create Even.js (name your own) The following is the code created in the JS file. There are two components of A, B, and JS that needs to be created i...

Vue event bus (EventBus) with caution

Usually suitable for brothers component value It is best to plan the scope of function -Profin the range of instance control (different instances in different areas) -An the scope of control space con...

Vue event bus (EVENTBUS) learning

1. 1. Background We know that there are many ways to transfer data between Vue components, such as:Father -child component communication,Intergenerational component communication,Routing,vuexWait, but...

The concept, use and attention of vue event bus EventBus

vue event bus Preface text Introduction to EventBus Use of EventBus One, initialization Two, send events to EventBus Three, receive events Fourth, remove the monitoring event Concluding remarks Prefac...

Use the central event bus eventBus in vue

Use the central event bus eventBus in vue Prepare to create files: router/index.js : App.vue : src/utils/eventBus.js : src/components/sky.vue : src/components/news.vue : src/views/About.vue Click on t...

More Recommendation

Talk about the event bus mechanism in vue-EventBus

Vue event bus mechanism-EventBus The background of the event bus The core part in vue is the component, and the communication between components is the top priority. Communication is divided into pare...

Vue Development Central Event Bus Eventbus

  Use background:   In Vue development, a large number of components are encountered, and VUE has a corresponding solution for different situations. For example, the parent component can use the ...

Vue implementation global Eventbus (global event bus)

Basic knowledge: There are three events on VUE prototypes: $ N (), $ EMIT (), $ OFF () The prototype object of the component object is a VM object: the component object can directly access the method ...

Vue in the event bus (Eventbus) detailed and used

Portal: Vue ion assembly to the parent components and .sync modifier Portal: Vue State Manager (Vuex) Detailed and Application Scene Portal: Vue $ Attrs, $ Listeners More 1 Introduction Frequently ava...

[VUE] The use of global event bus (Eventbus)

Table of contents Registration event bus use take over Destruction event bus Notice Registration event bus main.js use take over Destruction event bus Notice After using it, it is best to destroy in B...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top