Vue component development is a very wonderful process, because it reduces the coupling between codes and improves reusability, which is directly good news for us developers. However, components are components, and there are several different relationships between components. Different relationships require corresponding communication methods. For example, the relationship diagram between components is like the one below. There are more components than these, so how should these complex component communications be handled? Next, I will introduce two common component communication methods of Vue. Parent-child communication "props / $emit"From father to sonI want to pass data from the parent component to the child component, that is, from the app component to the A component in the figure above. I can first define an array in the App.vue component, and then bind it to the child component through v-bind. < template > Then receive it through props in the child component, and then render it in a loop! < template > Browser effect: From son to fatherTo pass parameters from a child component to a parent component, first we create a click event in the child component, and then we trigger the $emit event by clicking to send the value we want to pass. < template > Then we listen to the events of the child component through on in the parent component and receive the passed value and then trigger the event here, so as to achieve the purpose of passing from child to parent. < template > The browser displays the following: Brother CommunicationsBus communication is recommended for sibling communication because two components can communicate with each other directly, thus eliminating the two steps of passing information from child to parent and then to child. First, declare a bus, that is, create an EvenBus.js in a suitable place, and then the internal structure is as follows: import Vue from 'vue' Then some people may wonder why the Vue instance is introduced in this way. Let's continue reading with this question. Then I put two components inside the App component, BroderB.vue and BroderD.vue. APP.vue < template > Then let's look at BroderB.vue < template > I introduced EvenBus here, and then triggered the event through the click event, and then responded here why we need an instance, because each instance has an emit method, and of course a listening $on method. Then pass this event and value out. Then receive it in BroderD.vue < template > Listen to the $on event in the mounted hook function of this component and trigger the method here, so that the two components can communicate, and then the method here receives and uses the value Then some people may ask why there is a beforeDestroy hook function behind it. It must be useful. When we end this component, it is best to unbind the evenBus, because if it is in the project, there may be some strange problems. Then we look at the browser as follows |
>>: Operating system: Introduction to SFTP related knowledge
[[255972]] If you use the popular file explorer a...
[51CTO.com original article] On the afternoon of ...
Nowadays, the development of the Internet is real...
The current situation of NB-IoT (hereinafter refe...
There are many ways to read data from http.Reques...
Recently, the "Conference on Deepening the I...
In 2020, China, which was the first to achieve a ...
HostHatch is a foreign hosting company founded in...
BudgetVM is a local data center founded in 2015. ...
[[425909]] This article is reprinted from the WeC...
[[435412]] This article will talk about the collo...
Transforming digital experiences, products, servi...
Recently, Ms. He, a citizen, has frequently recei...
I believe that many people are excited about 5G n...
[ Dubai , UAE , October 11, 2023 ] During the Glo...