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>
相关推荐
拳打南山敬老院19 分钟前
Context 不是压缩出来的,而是设计出来的
前端·后端·aigc
用户30767528112723 分钟前
💡 从"傻等"到"流淌":我在AI项目中实现流式输出的血泪史(附真实代码+深度解析)
前端
bluceli24 分钟前
前端性能优化实战指南:让你的网页飞起来
前端·性能优化
UIUV25 分钟前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene26 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
没想好d27 分钟前
通用管理后台组件库-9-高级表格组件
前端
阿虎儿30 分钟前
React Hook 入门指南
前端·react.js
阿懂在掘金1 小时前
defineModel 是进步还是边界陷阱?双数据源组件的选择逻辑
vue.js·源码阅读
核以解忧1 小时前
借助VTable Skill实现10W+数据渲染
前端
WangHappy1 小时前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序