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

Analysis of data update scheme in Vue component communication

WBOY
WBOYOriginal
2023-07-19 10:33:311407browse

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
d477f9ce7bf77f53fbcf36bec1b69b7a
dc6dce4a544fdca2df29d5ac0ea9906b

<child-component :message="message" @update="handleUpdate"></child-component>

16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
import ChildComponent from './ChildComponent.vue';

export default {
components: {

ChildComponent

} ,
data() {

return {
  message: 'Hello World'
}

},
methods: {

handleUpdate(data) {
  this.message = data;
}

}
}
2cacc6d41bbb37262a98f745aa00fbf0

/ / ChildComponentChildComponent.vue
d477f9ce7bf77f53fbcf36bec1b69b7a
dc6dce4a544fdca2df29d5ac0ea9906b

<button @click="sendMessage">发送消息</button>

16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
export default {
props: ['message'],
methods: {

sendMessage() {
  this.$emit('update', 'Hello Vue');
}

}
}
2cacc6d41bbb37262a98f745aa00fbf0

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
d477f9ce7bf77f53fbcf36bec1b69b7a
dc6dce4a544fdca2df29d5ac0ea9906b

<child-component1 :message="message" @update-message="handleUpdateMessage"></child-component1>
<child-component2 :message="message"></child-component2>

16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
import ChildComponent1 from './ChildComponent1.vue';
import ChildComponent2 from './ChildComponent2.vue';

export default {
components: {

ChildComponent1,
ChildComponent2

},
data() {

return {
  message: 'Hello World'
}

},
methods: {

handleUpdateMessage(data) {
  this.message = data;
}

}
}
2cacc6d41bbb37262a98f745aa00fbf0

// Child component ChildComponent1.vue
d477f9ce7bf77f53fbcf36bec1b69b7a
dc6dce4a544fdca2df29d5ac0ea9906b

<button @click="sendMessage">发送消息</button>

16b28748ea4df4d9c2150843fecfba68
< ;/template>
3f1c4e4b6b16bbbd69b2ee476dc4f83a
export default {
props: ['message'],
methods: {

sendMessage() {
  this.$emit('update-message', 'Hello Vue');
}

}
}
< ;/script>

// Child component ChildComponent2.vue
d477f9ce7bf77f53fbcf36bec1b69b7a
dc6dce4a544fdca2df29d5ac0ea9906b

<p>{{ message }}</p>

16b28748ea4df4d9c2150843fecfba68
3f1c4e4b6b16bbbd69b2ee476dc4f83a
export default {
props: ['message']
}
2cacc6d41bbb37262a98f745aa00fbf0

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn