Home > Web Front-end > Vue.js > Analysis of data update scheme in Vue component communication

Analysis of data update scheme in Vue component communication

WBOY
Release: 2023-07-19 10:33:31
Original
1497 people have browsed it

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.

  1. Parent-child component communication

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

<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

<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.

  1. Sibling component communication

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

<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