Analysis of data update scheme in Vue component communication
In Vue development, component communication plays a vital role. In component communication, data update is an essential link. This article will analyze the data update scheme in Vue component communication and illustrate it through code examples.
In parent-child component communication, the parent component can pass data to the child component through props, and the child component can send data to the parent component through the event $emit. .
Code example:
// Parent component App.vue
<child-component :message="message" @update="handleUpdate"></child-component>
<script><br>import ChildComponent from './ChildComponent.vue';</p><p>export default {<br> components: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>ChildComponent</pre><div class="contentsignin">Copy after login</div></div><p>} ,<br> data() {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>return {
message: 'Hello World'
}</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div><p>},<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>handleUpdate(data) {
this.message = data;
}</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
/ / ChildComponentChildComponent.vue
<button @click="sendMessage">发送消息</button>
<script><br> export default {<br> props: ['message'],<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>sendMessage() {
this.$emit('update', 'Hello Vue');
}</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
In this example , the parent component passes the message attribute to the child component through props. When the child component clicks the button, an update event is sent to the parent component through the event $emit, and "Hello Vue" is passed as the data. After the handleUpdate method in the parent component receives the data, it assigns it to the message, thus updating the data.
In sibling component communication, you can define a data in the common parent component, and then update the data through props and events respectively.
Code example:
// Parent component App.vue
<child-component1 :message="message" @update-message="handleUpdateMessage"></child-component1>
<child-component2 :message="message"></child-component2>
<script><br>import ChildComponent1 from './ChildComponent1.vue';<br>import ChildComponent2 from './ChildComponent2.vue';</p><p>export default { <br> components: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>ChildComponent1,
ChildComponent2</pre><div class="contentsignin">Copy after login</div></div><p>},<br> data() {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>return {
message: 'Hello World'
}</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div><p>},<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>handleUpdateMessage(data) {
this.message = data;
}</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
// Child component ChildComponent1.vue
<button @click="sendMessage">发送消息</button>
< ;/template>
<script><br>export default {<br> props: ['message'],<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>sendMessage() {
this.$emit('update-message', 'Hello Vue');
}</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br>< ;/script></p><p>// Child component ChildComponent2.vue<br><template><br> <div></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><p>{{ message }}</p></pre><div class="contentsignin">Copy after login</div></div><p></div><br></template> ;<br><script><br>export default {<br> props: ['message']<br>}<br></script>
In this example, the parent component App A message data is defined in .vue and passed to two sub-components, ChildComponent1 and ChildComponent2. When ChildComponent1 clicks the button, an update-message event is sent to the parent component through the event $emit, and "Hello Vue" is passed as the data. After the handleUpdateMessage method in the parent component receives the data, it assigns it to the message. Since ChildComponent2 is also bound to the message property, ChildComponent2 will automatically update the display when the message is updated.
Summary
Through props and events, we can update data in Vue component communication. In parent-child component communication, the parent component passes data to the child component through props, and the child component sends a request to update data to the parent component through the event $emit. In sibling component communication, you can define a data in the common parent component, and then update the data through props and events. These data update solutions have been widely used in actual Vue development, helping us better implement component communication and data updates.
The above is the detailed content of Analysis of data update scheme in Vue component communication. For more information, please follow other related articles on the PHP Chinese website!