.sync 在vue 中相當於是更新v-on 屬性的監聽器
比如 我們寫
<Demoparent :foo.sync="barparam">
實際上是
<Demoparent :foo="barparam" @update:foo="val => barparam = val"></Demoparent>
子組件中修改foo的值的方法
this.$emit('update:foo', newValue)
當然前提是我們需要在子組件中的 props屬性中加入 foo : 例如
/*
*子組件代碼
*/
<template>
<div @click="changeValue">
{{foo}}
</div>
</template>
export default{
props:['foo'],
data(){
return{
}
},
methods:{
changeValue(){
this.$emit('update:foo', '已經觸發改變'); //更新foo的值
}
}