.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的值
}
}