eventbus

116 阅读1分钟

main.js

import EventBus from './plugins/eventBus'
Vue.prototype.$EventBus = EventBus

plugin-eventbus.js

import Vue from 'vue'
export const bus = new Vue()
export default bus 

传送的组件

import { bus } from '@/plugins/eventBus'
 beforeUnmount () {
   	bus.$emit('接收数据的组件名', this.要传送的数据)
   }

接收的组件

import { bus } from '@/plugins/eventBus'
  created () {
     let _this = this
     bus.$on('组件名', function (data) {
       _this.要展示的数据 = data
     })
   },