Vue.js use in the event of bus EventBus

tags: Vue.js  EventBus  Javascript front-end

1. Introduction and Installation

EventBus: Also known as the event bus. EventBus can be used in the Vue as a bridge concept is the same event as the center of all common components, you can register to receive or send events to the event center, so components can be up and down parallel to notify other components.
note: Very easy to use because there is no constraint management and retrieval programs, so when communication between any of the components, management and maintenance of the project will be extremely difficult to excessive bus, it is generally recommended as a better Vuex state management center, the concept of notice up to the shared state level.
installation: vue project executionnpm install vue-bus --save

2. Configure

Creating bus.js file src directory, add the code:

import vue from 'vue';
export default new vue();

3. Registration

Add the following code in main.js:

import Vue from 'vue';
// ======== registered a global event bus method
 import EventBus from 'vue-bus'; // central event bus
Vue.use(EventBus);
 // ======== registered global event bus Method Two
Vue.prototype.$EventBus = new Vue()
 // ======== registered global event bus Method Three
var EventBus = new Vue();
Object.defineProperties(Vue.prototype, {
  $bus: {
    get: function () {
      return EventBus
    }
  }
})

4. In the assembly

To the group to be introduced into the assembly components within a communication bus, so that belonging topical use.
when the global register main.js may not be required in the component is introduced as a global event bus use.

import EventBus from '../../bus.js'; // change the route according to the actual
// Send the array assembly
 . EventBus $ emit ( 'sendMsg', data); // called SENDMSG trigger event in the event of a bus and transmits data Data;
 // Accepts data within the bus
EventBus.$on("sendMsg", (msg) => {
   // Send a message to 
 console.log(msg);
  })
     Note:. EventBus $ off ( 'sendMsg') // remove all listeners of an event within this application.
     // or directly call EventBus. $ Off () to remove all event channels,
     // short before the destruction of the components must be removed vue bus, or will cause confusion and difficult to troubleshoot maintenance events,
   // recommended for bus destruction in vue of the beforeDestroy
   // When the project is a medium-sized or large projects recommended vuex do unified state management, event bus is suitable for components of communication under special circumstances.

Intelligent Recommendation

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...

Teach you to use the event bus EventBus

Teach you how to use EventBus EventBus: Also known as the event bus. In Vue, you can use EventBus as the concept of a communication bridge, just like all components share the same event center, you ca...

Basic Use of Android Event Bus Eventbus 3.0

I. Overview Android components, communication between threads, can be implemented with Handler, BroadcastReceiver, callback, etc., but the implementation of these methods is a cumbersome. Eventbus can...

[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...

More Recommendation

Detailed explanation and use of event bus (EventBus) in Vue

Plug -in method: No plug -in method:...

VUE's EventBus-Event bus bus

In Vue, the data transmission between components is generally passed on the parent -child component, but in the actual application of the project, there is also no relationship of components that need...

Vue event bus (EventBus)

Reference article: The core concept of many modern JavaScript frameworks and libraries is the ability to encapsulate data and UI in modular, reusable components. This allows developers to avoid writin...

ionic4-EventBus (event bus)

ionic4-EventBus (event bus) surroundings Welcome to visit me:Ionic4 column Foreword: AngularJs, we can use b r o a d c a s t 、 broadcast、 broadcast、On to send out, listen to the broadcast, to achieve ...

VUE event bus eventbus

table of Contents First, introduction to eventbus Second, the problem needs to be solved Third, how to use (1) Create a js file of EventBus (two) global call (3) Sending an event (4) Receiving events ...

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

Top