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)
}
})