vue3中设置响应式对象和数组

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
56
57
58
59
60
61
62
63
64
65
<template>
    <div class="persion">
        <h2>姓名:{{ name }}</h2>
        <h2>年龄:{{ age }}</h2>
        <h2>性别:{{ sex }}</h2>
        <button @click="nameTel">点击姓名</button>
        <button @click="ageTel">点击年龄</button>
        <button @click="showTel">点击显示电话</button>
        <li v-for="game in games" :key="game.id">{{ game.name }}</li>
        <button @click="games.push({id:'05',name:'和平三国'})">添加游戏</button>
        <button @click="updateGames">修改游戏</button>
 
        <h2>车名:{{ car.name }}</h2>
        <h2>价格:{{ car.price }}</h2>
        <button @click="carpricte">修改价格</button>
    </div>
</template>
<script lang="ts" setup name="Presion" >
import { ref } from 'vue';//针对一个数据变量
import { reactive } from 'vue';//针对数组,对象
 
 
let name = ref('张三');
        let age = ref(18);
let games = reactive([
        {id:'01',name:'王者农药'},
        {id:'02',name:'英雄联盟'},
        {id:'03',name:'和平精英'},
        {id:'04',name:'绝地求生'}
        ]);
let car = reactive({
            name:'奔驰',
            price:10000000,
        })
let sex = '男';
let tel = '13812345678';
     
function nameTel() {
            name.value = '李四';
            console.log(name);
        }
        //修改具体的参数,必须用value
        function ageTel() {
            age.value = age.value + 1;
        }
        function showTel() {
            alert(tel);
        }
        function updateGames() {
            games[0].name = '荒野求生';
        }
        //当需要同时修改多个对象时,用Object.assign(,,,)
        function carpricte() {
            Object.assign(car,{name:"宝马",price:"0"},games,games[0].name='荒野')
        }
 
</script>
<style>
.persion {
    background-color: skyblue;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
}
</style>

  

posted @   爱豆技术部  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
TOP
点击右上角即可分享
微信分享提示