vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定。
.sync 是一个语法糖, $event 获取了 $emit 的参数,$emit 可以触发事件,并传参。
<template>
<div class="app">
我有 {{total}}
<Child :money.sync="total"/> // 等价于 :money="total" v-on:update:money="total=$event"
</div>
</template>
<script>
import Child from "./Child.vue";
export default {
data(){
return {total:10000};
},
components:{
Child: Child
}
}
</script>
Child.vue
<template>
<div class="child">
{{money}}
<button @click="$emit('update:money',money-100)">
<span>花钱</span>
</button>
</div>
</template>
<script>
export default{
props:["money"]
}
</script>