写一个公共的中间人dridge. js:
import Vue from 'vue'
export default new Vue()
哥哥为发送方 header.vue:
<script>
import eventVue from './../api/bridge'
export default {
data() {
return {}
},
created() {},
computed: {},
methods: {
choice(command) {
eventVue.$emit('language', "zh");// 发送方 language为触发事件
}
}
}
</script>
弟弟为接收方tail.vue: 必须在 mounted周期接收
<script>
import eventVue from './../api/bridge'
export default {
name: 'tail',
data() {
return {}
},
mounted() {
this.getCache();
eventVue.$on('language', (arg) => { // language必须要和哥哥发送方事件一致
console.log("传递过来的参数为:"+arg) // 接收
});
},
computed: {},
methods: {}
}
</script>