5、reactive

作用

reactive`用来定义:对象类型数据

reactive重新分配一个新对象,会失去响应式(可以使用Object.assign`去整体替换)。

代码

复制代码
<template>
<div>
    
    <h2>{{ info.name }}</h2>
    <h2>{{ info.age }}</h2>
    <button @click="updateName">修改名字</button>
    <button @click="updateAge">修改年龄</button>
    <button @click="showPhone">显示联系方式</button>
</div>
</template>

<script setup lang="ts" name="Student">
    import { reactive } from 'vue';
    let info=reactive({name:'weiwei',age:18})
    function updateName(){
        info.name="李四"
    }
    function updateAge(){
        info.age+=1
        
    }
    function showPhone(){
        alert("18880709")
    }
</script>

<style scoped>

</style>

重新分配一个新对象代码

复制代码
<template>
<div>
    
    <h2>{{ info.name }}</h2>
    <h2>{{ info.age }}</h2>
    <button @click="updateName">修改名字</button>
    <button @click="updateAge">修改年龄</button>
    <button @click="updateInfo">修改名字和年龄</button>
    <button @click="showPhone">显示联系方式</button>
</div>
</template>

<script setup lang="ts" name="Student">
    import { reactive } from 'vue';
    let info=reactive({name:'weiwei',age:18})
    function updateName(){
        info.name='李四'
    }
    function updateAge(){
        info.age+=1
        
    }
    function showPhone(){
        alert("18880709")
    }
    function updateInfo(){
        Object.assign(info,{name:'王五',age:38})
    }
</script>

<style scoped>

</style>
相关推荐
用户新13 小时前
JS事件深度解析四 事件的循环和异步
前端·javascript·事件·event loop
广州灵眸科技有限公司19 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) Easy-Eai编译环境准备与更新
服务器·前端·人工智能·python·深度学习
万少20 小时前
我把 Kimi 接进微信,几分钟做了个随手出图助手
前端
xiaofeichaichai20 小时前
网络请求与实时通道
前端·网络
kTR2hD1qb21 小时前
从 Responses API 到 Chat Completions:一个模型网关的设计复盘
linux·前端
kyriewen1 天前
浏览器缓存最强攻略:强缓存、协商缓存、CDN、更新策略,一篇搞定
前端·面试·浏览器
持敬chijing1 天前
Web渗透之SQL注入-联合查询注入-注入点数据类型判断
前端·sql·安全·web安全·网络安全·安全威胁分析
卷帘依旧1 天前
Web3前端一面
前端
古韵1 天前
告别手写分页逻辑:usePagination 从 50 行到 3 行
java·前端