When we need to change only the child component and the parent component can listen for that event and update a local data property as needed, we can use.sync, which is extended to a V-on listener that automatically updates the parent component’s property.

<comp :foo.sync="bar"></comp>
Copy the code

The complete form is:

<comp :foo="bar" @update:foo="val => bar = val"></comp>
Copy the code

For example:

<template>
    <div class="details">
        <myComponent :show.sync='valueChild' style="padding: 30px 20px 30px 5px; border:1px solid #ddd; margin-bottom: 10px;"></myComponent>
        <button @click="changeValue">toggle</button>
    </div>
</template>
<script>
import Vue from 'vue'
Vue.component('myComponent', {
      template: `<div v-if="show"> <p> Default initial value is {{show}}, so it is displayed </p> < [email protected] ="closeDiv"</button> </div> ', props:['show'],
      methods: {
        closeDiv() {
          this.$emit('update:show'.false); // Trigger the input event and pass in a new value}}})export default{
    data() {return{
            valueChild:true,
        }
    },
    methods:{
        changeValue(){ this.valueChild = ! this.valueChild } } } </script>Copy the code

When a child component changes the value of a prop, the change is also synchronized to the parent component binding.