创建一个JS文件 eventBus.js
import Vue from ‘vue‘ const eventBus = new Vue() export default eventBus
使用时 在 要使用的组件中 import 引入 eventBus.js
使用 $emit 传递自定义方法和参数 就和 子组件传父组件一样
如下 传了一个自定义方法 addItem 和参数 inputValue
eventBus.$emit(‘addItem‘, this.inputValue)
在要接收的兄弟元素中同样引入 eventBus.js
触发自定义方法 addItem 同时触发自身的方法 handleEvent
eventBus.$on(‘addItem‘, this.handleEvent)
handleEvent 中的参数即是之前 传递的参数 inputValue
handleEvent (value) { 一些代码... // value即是传递的inputValue }
这样即完成了兄弟组件间的传值
PS: 为什么我感觉没有vuex好用呢
原文:https://www.cnblogs.com/shirunfeng/p/14826590.html