【发布时间】:2019-10-15 09:06:32
【问题描述】:
我在 laravel 中使用 vue js,需要使用全局事件总线。我创建了 event-bus.js,并将它导入到我需要使用的地方;当我单击时,会生成事件,但听众没有反应。我尝试了一切,但它不起作用。请帮帮我,我已经工作了3天了
我尝试在 app.js 中创建一个 eventBus,但也没有用
我的 EditDiscountComponent
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();
},
我用不同的方法尝试了函数的回调,但没有结果,我在mount()中尝试了这个监听器,这没有帮助
【问题讨论】:
-
尝试将其导入为 import EventBus from '@/eventBus'
-
这个'@/eventBus'是什么意思?
标签: javascript laravel vue.js