利于组件之间通信相当于一个收发室
创建:
import Vue from 'vue'
const bus = new Vue()
// 清除监听事件
bus.$clear = function (types = []) {
let that = this
if (types) {
if (typeof types === 'string') {
types = [types]
}
if (types instanceof Array) {
for (let type of types) {
that.$off(type)
}
}
}
}
// 注册监听事件
bus.$register = function (type, callback, that, field) {
if (that) {
let types = field || 'types'
if (!that[types]) {
that[types] = {}
}
that[types][type] = true
}
bus.$on(type, callback)
}
export default bus
发:that.$utils.bus.$emit('platform/startMenu/hide',123)
收:that.$utils.bus.$on('platform/startMenu/hide',function(data){data++})
使用一次:.$once( event, callback )
关闭:$off( [event, callback] )
移除自定义事件监听器。
- 如果没有提供参数,则移除所有的事件监听器;
- 如果只提供了事件,则移除该事件所有的监听器;
- 如果同时提供了事件与回调,则只移除这个回调的监听器。
- 一般放在beforeDestroy中进行处理
不像vuex一样有这管理追踪系统仅仅是收发
commit
因为都是数据管理 所以于eventBus的$emit类似
dispatch
当所提交的数据涉及异步时要用dispatch提交
模块下的使用方式commit同理
store.dispatch('user/getInfo')