


Detailed explanation of eventBus implementation of sibling component communication in vue2.0s
This article mainly introduces the example code of eventBus in vue2.0s to implement sibling component communication. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
In vue1.0, communication between components is mainly achieved through vm.$dispatch propagating upward along the parent chain and vm.$broadcast downward broadcasting. However, in vue2.0, this usage has been abolished.
After the addition of vuex, the communication between components will be clearer. For medium and large projects, it is a wise choice to plan the use of vuex from the beginning.
However, in some small projects, or for people like me who find out that vue2.0 cannot use $.broadcast and $dispatch halfway through writing, a more convenient solution is needed. . Then, the role of eventBus is reflected.
The main practical approach is to introduce a new vue instance among the sibling components that want to communicate with each other, and then realize communication and parameter transfer by calling the event triggering and monitoring of this instance respectively.
Here is a simple example:
For example, we have three components here, main.vue, click.vue, and show.vue. click and show are sibling components under the parent component main, and click traverses multiple list items in the parent component through v-for. What needs to be implemented here is that after the click event is triggered in the click component, the show component will console which DOM element was clicked.
First, we add a click event to the click component
<p class="click" @click.stop.prevent="doClick($event)"></p>
We want to implement the show component in the doClick() method For communication, we need to create a new js file to create our eventBus. We name it bus.js
import Vue from 'vue'; export default new Vue();
so that we create a new vue Example. Next we import it in the click component and show component.
import Bus from 'common/js/bus.js';
Next, we trigger an event in the doClick method:
methods: { addCart(event) { Bus.$emit('getTarget', event.target); } }
Here we click Every time you click in the component, the event named 'getTarget' will be triggered in the bus, and the event.target of the click event will be passed along the event.
Next, we need to call the bus in the created() hook in the show component to listen for this event and receive the parameters:
created() { Bus.$on('getTarget', target => { console.log(target); }); }
This way , in each click event of the click component, the event.target will be passed to the show and consoled.
So the use of eventBus is still very convenient, but if it is a medium to large project and the communication is more complicated, it is recommended that you use vuex directly.
Related recommendations:
Vue brother component communication method
Vue.js component communication example sharing
Detailed explanation of event bus non-parent-child component communication in vue
Detailed analysis of several postures in Vue.js component communication
Deep dive into Vue.js components and component communication
The above is the detailed content of Detailed explanation of eventBus implementation of sibling component communication in vue2.0s. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

Vue is one of the most popular front-end frameworks currently, and VUE3 is the latest version of the Vue framework. Compared with VUE2, VUE3 has higher performance and a better development experience, and has become the first choice of many developers. In VUE3, using extends to inherit components is a very practical development method. This article will introduce how to use extends to inherit components. What is extends? In Vue, extends is a very practical attribute, which can be used for child components to inherit from their parents.

How does Vue dynamically render components through JSX? The following article will introduce to you how Vue can efficiently dynamically render components through JSX. I hope it will be helpful to you!

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

When developing Vue/React components in VSCode, how to preview the components in real time? This article will share with you a plug-in for real-time preview of Vue/React components in VSCode. I hope it will be helpful to you!
