为什么 Vue 全局事件总线在我的项目中不起作用



我在 laravel 中使用 vue js,我需要使用全局事件总线。我创建了event-bus.js,并将其导入到我需要使用它的地方;当我单击时,会生成事件,但侦听器没有反应。我尝试了所有方法,但它不起作用。请帮助我,我已经工作了 3 天

我尝试在 app.js 中创建事件总线,但它也没有帮助

我的编辑折扣组件

import { EventBus } from "../../../event-bus";
editDiscount () {
const data = {
discount_id: this.discountData.discount_id,
status: this.discountData.status,
type: this.discountData.type,
percentage:this.discountData.percentage,
amount:this.discountData.amount,
};
EventBus.$emit('update-discount', data);
this.languages.forEach(lang => {
if (lang.code && this.discountData[lang.code] !== undefined) {
data[lang.code] = this.discountData[lang.code];
}
});
this.$store.dispatch(actions.EDIT_DISCOUNT, data)
.then(res => {
if (res && res.data.status) {
// window.location.href = '/admin/discounts';
} else {
this.$store.commit(mutations.SET_SNACKBAR_SHOW, true);
this.$store.commit(mutations.SET_SNACKBAR_TEXT, res.data.message);
}
}).catch(console.error);
},
},

我的产品供应商


import { EventBus } from "../../../event-bus";
mounted() {
EventBus.$on('update-discount', ($data) => {
console.log($data);
});
this.getCategories();
this.getProducts();
},

我尝试使用不同的方法回调函数,但没有结果,我在挂载((中尝试了这个侦听器,这没有帮助

您可以在项目的文件夹中创建eventBus.js文件src

如下所示
import Vue from 'vue'
export default new Vue()

并尝试导入它,如下所示:

import EventBus from '@/eventBus'

或者这可能有助于您 https://alligator.io/vuejs/global-event-bus/

最新更新