SoFunction
Updated on 2025-04-05

Detailed explanation of the usage of sync syntax sugar in vue

In Vue, the use of .sync syntax sugar is as follows:

In the parent component, the prop of the child component is used to bind the data of the parent component in two-way, and the data of the child component is monitored in the child component by listening for the changes of a prop passed by the parent component. When the prop value changes, the child component will issue a custom update event and pass a new value to the parent component.

Vue's .sync syntax sugar is an instruction for bidirectional data binding. It can be used in child components to listen for changes in props passed by the parent component, and issue a custom event to notify the parent component to update data when needed.

Here is a sample code:

<!-- Parent component -->
<template>
<div>
<input v-model="message" />
<child-component :my-message="message" @update:my-message="updateMessage" />
</div>
</template>
 
<script>
import ChildComponent from './';
 
export default {
data() {
return {
message: ''
};
},
components: {
ChildComponent
},
methods: {
updateMessage(newMessage) {
 = newMessage;
}
}
};
</script>

In child components, use the v-model directive to bind the child component's prop to the child component's data in two-way, and issue a custom update event when needed to notify the parent component to update data.

Here is a sample code:

<!-- Subcomponents -->
<template>
<div>
<input v-model="message" />
</div>
</template>
 
<script>
export default {
props: ['myMessage'],
data() {
return {
message:  // Bind the prop value to the message in data to implement two-way binding};
},
watch: {
message(newMessage) { // Listen to the message changes. When the message changes, a custom update event is issued, and a new value is passed to the parent component.this.$emit('update:my-message', newMessage);
}
}
};
</script>

The above is a detailed explanation of the usage of sync syntactic sugar in vue. For more information about vue sync syntactic sugar, please pay attention to my other related articles!