js_reverse/学习VUE/hello_vue3/笔记/computed计算属性.vue
2024-10-30 17:55:48 +08:00

46 lines
849 B
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="person">
<input type="text" v-model="firstName"><br>
<input type="text" v-model="lastName"><br>
全名<span>{{fullName}}</span>
<button @click="changeFullName">修改全名</button>
</div>
</template>
<script setup>
import { ref, computed } from "vue";
let firstName = ref('涨')
let lastName = ref('三')
// 只读的计算属性
// let fullName = computed(()=>{
// return firstName.value + lastName.value
// })
// 可读可写计算属性
let fullName = computed({
get(){
return firstName.value + lastName.value
},
set(val){
console.log(val)
firstName.value = 'dddddd'
}
})
function changeFullName(){
fullName.value = 'dddddd'
}
</script>
<style scoped>
.person {
background-color: antiquewhite;
box-shadow: 0 0 10px;
}
button {
margin: 5px;
}
</style>