Home  >  Q&A  >  body text

javascript - vue data transfer problem

app.vue page

bPage

I want page b to trigger the custom event of app.vue,

But it says bus is not defined. I want to know how to introduce it?

迷茫迷茫2711 days ago547

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:47:06

    bus is brought up and placed in a specific place.

    1. Create bus.js

    // bus.js
    import Vue from 'vue';  
    export default new Vue(); 

    2. Statement

    import Bus from 'bus';  
    
    export default {
        created() {  
            Bus.$on('getData', target => {  
                console.log(target);  
            });  
        }  
    }

    3. Call

    import Bus from 'bus';  
    
    export default{
        methods: {  
           but(event) {  
               Bus.$emit('getData', event.target);   
           }  
        }  
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:47:06

    Although this approach is not highly recommended, you can:

    window.bus = new Vue();

    You can’t access bus because bus is a local variable. . .


    Added:

    If you do not use the global event bus, you can use the plug-in method: vue-bus

    In addition, if you want to share this data between multiple components, use Vuex

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:47:06

    Just put it in the data
    It’s not clear yet, look here

    reply
    0
  • Cancelreply