兄弟组件传参 vue

写一个公共的中间人dridge. js:

1 import Vue from 'vue'
2 export default new Vue()

哥哥为发送方 header.vue:

 1 <script>
 2     import eventVue from './../api/bridge'
 3     export default {
 4         data() {
 5             return {}
 6         },
 7         created() {},
 8         computed: {},
 9         methods: {
10             choice(command) {
11                 eventVue.$emit('language', "zh");// 发送方  language为触发事件
12             }
13         }
14     }
15 </script>

弟弟为接收方tail.vue: 必须在 mounted周期接收

 1 <script>
 2     import eventVue from './../api/bridge'
 3     export default {
 4         name: 'tail',
 5         data() {
 6             return {}
 7         },
 8         mounted() {
 9             this.getCache();
10             eventVue.$on('language', (arg) => { // language必须要和哥哥发送方事件一致
11                 console.log("传递过来的参数为:"+arg) // 接收
12             });
13         },
14         computed: {},
15         methods: {}
16     }
17 </script>

原文地址:https://www.cnblogs.com/xiaozhu-zhu/p/11946884.html