突然,发射器停止工作:
event-bus.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
});
}
},another-component.vue
mounted() {
// MiniCart.vue
EventBus.$on('cartLoaded', (payload) => {
...
});
},无论我如何尝试在mounted/created中发出事件,它都不会工作。没有问题,当触发事件的点击或其他。
创建沙箱:https://codesandbox.io/s/gracious-kilby-m43ih?fontsize=14&hidenavigation=1&theme=dark
发布于 2020-10-30 21:31:39
发布于 2020-10-30 11:50:04
根据这,您应该使用kebab-case格式来命名自定义事件:
EventBus.$emit('cartLoaded', this.cart);//not correct
EventBus.$emit('cart-loaded', this.cart); //correct发布于 2020-10-30 20:49:47
可能是在MiniCart.vue组件注册事件之前发出的事件。
在代码中的意思。
EventBus.$emit('cartLoaded', this.cart);此运行在事件注册之前第一次运行。
EventBus.$on('cartLoaded', (payload) => {
...
});https://stackoverflow.com/questions/64607638
复制相似问题