首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Vue全局EventBus在我的项目中不起作用

为什么Vue全局EventBus在我的项目中不起作用
EN

Stack Overflow用户
提问于 2019-10-15 17:06:33
回答 1查看 840关注 0票数 0

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

我尝试在app.js中创建一个eventBus,但也没有帮助

我的EditDiscountComponent

代码语言:javascript
复制
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

代码语言:javascript
复制
import { EventBus } from "../../../event-bus";

 mounted() {
            EventBus.$on('update-discount', ($data) => {
                console.log($data);
            });
            this.getCategories();
            this.getProducts();
        },

我使用另一种方法尝试了函数的回调,但没有结果,并且我在mount ()中尝试了此侦听器,但这没有帮助

EN

回答 1

Stack Overflow用户

发布于 2019-10-15 17:27:04

您可以在项目的src文件夹中创建eventBus.js文件,如下所示

代码语言:javascript
复制
import Vue from 'vue'
export default new Vue()

并尝试导入,如下所示:

代码语言:javascript
复制
import EventBus from '@/eventBus'

或者这可能对你有帮助,https://alligator.io/vuejs/global-event-bus/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58391033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档