this.$EventBus.$on
时间: 2023-10-28 10:02:13 浏览: 94
this.$EventBus.$on is a method used in Vue.js to listen for custom events. It allows components to communicate with each other by sending and receiving events.
When calling this.$EventBus.$on, you need to provide the event name as the first argument and a callback function as the second argument. The callback function will be executed when the event is emitted.
For example:
```javascript
// Component A
this.$EventBus.$on('customEvent', (data) => {
console.log('Received event:', data);
});
// Component B
this.$EventBus.$emit('customEvent', 'Hello from Component B!');
```
In the above example, Component A is listening for the 'customEvent' and will log the received data when the event is emitted from Component B using this.$EventBus.$emit.
阅读全文