Vue eventBus event bus

After the project is over, the state management or component value is often used. It is more troublesome to use the traditional method, so there is a big invented bus stuff, here is a summary.

1. First create a new bus.js file with the following contents;
const install = function (Vue) {
const Bus = new Vue({
    methods:{
                 / / Define the method: (here ... is the extension operator in the es6 syntax, do not understand the small partner can Baidu)
        emit(event,...args){
            this.$emit(event,...args);
        },
        on(event,callback){
            this.$on(event,callback);
        },
        off(event,callback){
            this.$off(event,callback);
        }
    }
});
 / / Register to add a global attribute to the prototype of the vue object
Vue.prototype.$bus = Bus;
};
export default install;

 2. Introduce bus.js in main.js and use it.
import Bus from './store/bus'

Vue.use(Bus)
 This can be used in the component. When using it, you need to pass the this.$bus. method (ie, emit, on, off), you can pass strings, arrays, functions, etc...

 Use:
 Component one: delivery;
export default {
    name:'Home',
    methods:{
        init(){
            let arr1 = ['hi~'];
            function aa(){
                return arr1;
            }
            let str = 'hello';
            let arr = [2,3,4,5];
            this.$bus.$emit('log',aa())
            this.$bus.$emit('log1',str)
            this.$bus.$emit('log2',arr)
        },
    },
    created() {
        this.init();
    },
}

 Component 2: Receiving
export default {
    name:'about',
    methods:{
        change(){
            this.$bus.$emit('log',5);
        }
    },
    mounted() {
                 //This is to clear some first, it is said to solve the problem of repeated registration. (Whether it is written first, just in case)
        this.$bus.$off('log')
        this.$bus.$off('log1')
        this.$bus.$off('log2')
        this.$bus.$on('log',(res)=>{
            console.log(res);
        })
        this.$bus.$on('log1',(res)=>{
            console.log(res);
        })
        this.$bus.$on('log2',(res)=>{
            console.log(res);
        })
    },
}Copy code

Reprinted at: https://juejin.im/post/5c418635f265da611d66ebe2

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