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 40 41 42 43 44 45 46 47
| <template> <button @click="changeAge">用户年龄+1</button> <button @click="changeAddress">修改用户地址</button> <button @click="changeHobbys">修改用户爱好</button>
<h2>用户名</h2>{{ user.name }} <h2>用户年龄</h2>{{ user.age }} <h2>爱好</h2>{{ user.hobbys }} </template>
<script setup> import { ref, reactive, computed, watch, watchEffect } from "vue";
let user = reactive({ name: "张三", age: 18, hobbys: ["吃饭", "睡觉", "打豆豆"], address: { province: "浙江省", city: "杭州市", county: "西湖区" } })
watchEffect((aaa) => { console.log("userAge可能改变了", user.age); console.log("userHobby可能改变了", user.hobbys[0]); console.log("userAddress可能改变了", user.address.city); })
function changeHobbys() { user.hobbys[0] = "打游戏"; }
function changeAge() { user.age++; }
function changeAddress() { user.address.city = "温州市"; } </script>
|