?computed可以接受一个get、set函数的对象,变成可写的computed(创建一个可写的 ref 对象)。
<script setup lang="ts">
import { ref, computed } from "vue"
const count = ref(1)
// const plusOne = computed(() => count.value + 1) 只读的计算属性ref
const plusOne = computed({ // 可写的计算属性 ref
get:() => count.value + 1,
set:(val) =>{
count.value = val - 1
}
})
/**
* Make the `plusOne` writable.
* So that we can get the result `plusOne` to be 3, and `count` to be 2.
*/
plusOne.value++
</script>
<template>
<div>
<p>{{ count }}</p>
<p>{{ plusOne }}</p>
</div>
</template>