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 48 49 50 51 52 53 54 55
| <script setup lang="ts"> import { reactive } from 'vue'; // 导入 reactive 方法, 用于只能创建响应式对象和ref的区别就是,ref是单个值,reactive是对象 // 数据 let car = reactive({ brand: '奔驰', price: 100 }) let games = reactive([{ id: '1', name: '王者荣耀' }, { id: '2', name: '英雄联盟' }, { id: '3', name: '和平精英' }]) let obj = reactive({ a: { b: { c: 100 } } }) // 方法 function changeGame1() { games[0].name = '和平精英' } function changeCar() { Object.assign(car, { brand: '兰博基尼', price: 300 }) // Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。 } function changeName() { car.brand = '宝马' } function changePrice() { car.price += 10 // 修改响应式对象中的属性,会触发视图更新 } function abc() { obj.a.b.c = 200 } </script>
<template> <div class="student"> <h2>汽车信息{{ car.brand }}, 价值{{ car.price }}</h2> <button @click="changePrice">涨价</button> <button @click="changeName">换车</button> <button @click="changeCar">换整辆车</button> <!-- <br /> --> <h2>游戏列表</h2> <ul> <li v-for="item in games" :key="item.id">{{ item.name }}</li> <!-- v-for 循环遍历 --> </ul> <button @click="changeGame1">修改游戏1</button> <h2>测试{{ obj.a.b.c }}</h2> <button @click="abc">测试 </button> </div>
</template>
<style scoped> .student { background: #ccc; padding: 20px; box-shadow: 0 0 10px skyblue; border-radius: 10px; margin: auto; }
li { font-size: bolder; } </style>
|