如果我们需要双向绑定数据
<p><input v-model = "fullName" /></p>
export default{
setup(){
const person = reactive({
firstName:'朱',
lastName:'先生'
})
person.fullName = computed({
get(){
// 获取数据的时候会主动调用 get 函数,拿到结果
return person.firstName + ' ' + person.lastName
},
set(newValue){
// 设置数据的时候会主动调用 set 函数,newValue就是修改后的新值
const arr = newValue.split(' ')
person.firstName = arr[0]
person.lastName = arr[1]
}
})
return {
...toRefs(person)
}
}
}