如何解决我的 eventBus 实现有什么问题?
我想从另一个不是父子关系的组件触发一个函数。我尝试使用 eventbus,但我不确定我是否正确实现了它,因为该功能不起作用。我创建了一个启动文件 eventBus.js 并将其添加到类星体配置中,但它仍然无法正常工作。任何人都可以帮我检查一下我执行错误的部分是什么?
eventBus.js
import Vue from 'vue'
const eventBus = new Vue();
export default eventBus
Vue.prototype.$eventBus = eventBus;
下面是我想从这个页面获取的函数,(this.getSchedule(),它在当前页面上完美运行)。
mounted(){
this.$eventBus.$on("refreshListSchedule",()=>{
scheduleDate = this.scheduleDate
console.log(scheduleDate)
console.log("Testing successed")
this.getSchedule()
})
},
我想触发它的页面
this.$eventBus.$emit('refreshListSchedule')
解决方法
最好在每个要发出或订阅事件的组件中从 eventBus.js
手动导入 - 不要依赖 Vue.prototype。
并且不要忘记在 beforeDestroy
挂钩中取消订阅。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。