vue, 计算属性如何赋值?-灵析社区

桃子爱吃玉米

问个问题, vue computed不建议赋值, 因为computed是个只读对象, 但是computed不是有个set吗, 我现在在composition api, setup语法糖中想使用set并且给computed赋值该怎么做呢

阅读量:16

点赞量:0

问AI
你是否是要这样: 计算属性 修改计算属性 今年:{{age}}岁了 明年:{{nextAge}}岁了 import {ref,computed} from 'vue' excort default { name:"App", setup(){ const age=ref(18) const nextAge=computed({ get(){ // 读取计算属性的值,调用get方法 return age.value+1 }, set(v){ // 修改计算属性的值,调用set方法 age.value=v-1 } }) retuen { age , nextAge} }