近期对Vue中ip的部分理解(以及$emit、$on、$once、$off、commit、dispatch)

利于组件之间通信相当于一个收发室

创建:

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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值