【发布时间】:2021-02-12 20:56:02
【问题描述】:
出乎意料,发射器停止工作:
事件总线.js
import Vue from 'vue';
export const EventBus = new Vue();
import { EventBus } from '../event-bus';
...
mounted() {
this.getCart();
}
...
methods: {
getCart() {
axios.get(`${app.api_erp_url}/cart/${this.cartId}`).then((response) => {
this.cart = response.data;
EventBus.$emit('cartLoaded', this.cart); // this not working
});
}
},
另一个组件.vue
mounted() {
// MiniCart.vue
EventBus.$on('cartLoaded', (payload) => {
...
});
},
无论我如何尝试在mounted/created 中发出事件,它都不起作用。触发点击事件时没有问题。
创建的沙盒:https://codesandbox.io/s/gracious-kilby-m43ih?fontsize=14&hidenavigation=1&theme=dark
【问题讨论】:
-
将调试器放在上面一行 - 检查 EventBus 事件数组 - 如果为空,其他组件尚未注册任何内容,如果调试器从未触发 axios throws
-
你确定
axios.get中没有error吗? -
是的,我确定:codesandbox.io/s/…
-
@RamkatLTU 尝试在
created:hook而不是mouted中添加监听器。我有一种感觉,在event是$emitted之前,您的$on可能还没有“到位”
标签: javascript vue.js vuejs2