computed
<template>
<div id="app">
<p>姓名:{
{ name }}</p>
<p>年龄:
<button type="button" @click="changeAge(-1)">-</button>
{
{ age }}
<button type="button" @click="changeAge(1)">+</button>
</p>
<p>出生年份{
{year}}</p>
</div>
</template>
<script>
import { ref ,computed} from "vue";
export default {
setup() {
const name = ref("测试");
const age=ref(18);
const year=computed(()=>{
return 2021-age.value;//计算
})
function changeAge(val){
age.value +=val;
}
return { name ,age,changeAge,year};
},
};
</script>
<style >
</style>