我在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);
},
},我的ProductCOmponent
import { EventBus } from "../../../event-bus";
mounted() {
EventBus.$on('update-discount', ($data) => {
console.log($data);
});
this.getCategories();
this.getProducts();
},我使用另一种方法尝试了函数的回调,但没有结果,并且我在mount ()中尝试了此侦听器,但这没有帮助
发布于 2019-10-15 17:27:04
您可以在项目的src文件夹中创建eventBus.js文件,如下所示
import Vue from 'vue'
export default new Vue()并尝试导入,如下所示:
import EventBus from '@/eventBus'或者这可能对你有帮助,https://alligator.io/vuejs/global-event-bus/
https://stackoverflow.com/questions/58391033
复制相似问题