Vue components communication

前端 未结 6 1387
臣服心动
臣服心动 2020-12-01 09:18

I have two Vue components:

Vue.component(\'A\', {});

Vue.component(\'B\', {});

How can I access component A from component B? How does the

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 10:01

    Communication between the components can also be established by creating a single global event hub in your Vue application. Something like this:-

    var bus = new Vue();

    Now you can create custom events and listen to them from any component.

         // A.vue
        // ...
        doThis() {
            // do the job
    
            bus.$emit('done-this');
        }
    
        // B.vue
        // ...
           method:{
                 foo: function()
              }
        created() {
            bus.$on('done-this', foo);
        }
    

    More about this can be found from official documentation..

提交回复
热议问题