javascript - vue data transfer problem
迷茫
迷茫 2017-05-19 10:45:06
0
3
541

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?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
滿天的星座

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);   
       }  
    }  
}
Ty80

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

仅有的幸福

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!