创建event-bus.js文件,内容如下:
import Vue from 'vue';
export const EventBus = new Vue();
在父子组件内分别引入:
import { EventBus } from './js/event-bus';
父组件定义触发后的操作,或传的值:
created() {
EventBus.$on('refreshTreeKey', () => {
this.treeKey++;
});
},
destroyed() {
EventBus.$off('refreshTreeKey');
},
或
created() {
EventBus.$on('currentRow', (data) => {
this.currentRow = data;
});
},
destroyed() {
EventBus.$off('currentRow');
},
子组件触发父组件自定义事件:
EventBus.$emit('refreshTreeKey');
或
EventBus.$emit('currentRow', this.currentRow);