1. 【computed】
作用:根据已有数据计算出新数据(和Vue2
中的computed
作用一致)。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <template> <div class="person"> 姓:<input type="text" v-model="firstName"> <br> 名:<input type="text" v-model="lastName"> <br> 全名:<span>{{fullName}}</span> <br> <button @click="changeFullName">全名改为:li-si</button> </div> </template>
<script setup lang="ts" name="App"> import {ref,computed} from 'vue'
let firstName = ref('zhang') let lastName = ref('san')
let fullName = computed({ get(){ return firstName.value + '-' + lastName.value }, set(val){ console.log('有人修改了fullName',val) firstName.value = val.split('-')[0] lastName.value = val.split('-')[1] } })
function changeFullName(){ fullName.value = 'li-si' } </script>
|