vue在路由中使用全局事件总线

1、routers下单index.js

var _this = ''
// 获取原型对象上的push函数
const originalPush = Router.prototype.push
// 修改原型对象中的push方法
Router.prototype.push = function push (location) {
  // 如果重复点击小说管理,则发送总线
  if (location == '/manageindex/storymanage') {
    let obj = {
      type: 'story_home'
    }
    _this.bus.$emit('story_manage', obj)
  }
  return originalPush.call(this, location).catch(err => err)
}

function toto (that) {
  // 获取全局实例
  _this = that
}
export {
  router,
  toto
}

main.js

import {router, toto} from './routers'

Vue.prototype.bus = new Vue() // 全局的事件总线对象

/* eslint-disable no-new */
var aaa = new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>'
})
// aaa相当于this
toto(aaa)

在组件中接收

// 获取总线
    let that = this
    that.bus.$on('story_manage', function (data) {
      console.log(data)
      if (data.type === 'close_pup') {
        that.$store.commit('changeDialog', false)
      }
    })

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值