reactive创建对象类型的响应式数据

reactive创建对象类型的响应式数据

在 Vue 3 的 Composition API 中,reactive() 函数用于创建对象类型**(对象、数组、Map、Set 等)的响应式数据。**

  • 和ref一样,在使用reactive之前,我们需要进行引入
js 复制代码
import { reactive } from 'vue'
  • 之后,也和ref的用法差不多,用reactive创建响应式数据
js 复制代码
  const user = reactive({
        name: '张三',
        age: 25,
        address: {
            city: '北京',
            street: '朝阳区'
        }
    })
js 复制代码
<template>
    <div class="person">
        <h2>{{ user.name }}</h2>
        <p>年龄: {{ user.age }}</p>
        <p>城市: {{ user.address.city }}</p>
    </div>
</template>
  • 让我们添加两个按钮,来修改验证数据是不是响应式的
js 复制代码
<template>
  <div>
    <h2>{{ user.name }}</h2>
    <p>年龄: {{ user.age }}</p>
    <p>城市: {{ user.address.city }}</p>
    
    <button @click="incrementAge">增加年龄</button>
    <button @click="updateCity">更新城市</button>

  </div>
</template>
js 复制代码
<script lang="ts" setup>
    import { reactive } from 'vue'
    const user = reactive({
        name: '张三',
        age: 25,
        address: {
            city: '北京',
            street: '朝阳区'
        }
    })
    const incrementAge = () => {
    user.age++
    }

    const updateCity = () => {
    user.address.city = '上海'}


 </script>
  • 那我们如何读取修改或者添加到reactive中的响应式数据呢?
js 复制代码
  const todos = reactive([
        { id: 1, text: '学习 Vue 3' },
        { id: 2, text: '掌握响应式' }
    ])
js 复制代码
<template>
  <div>
    <h2>{{ user.name }}</h2>
    <p>年龄: {{ user.age }}</p>
    <p>城市: {{ user.address.city }}</p>
    
    <button @click="incrementAge">增加年龄</button>
    <button @click="updateCity">更新城市</button>
    
    <ul>
      <li v-for="todo in todos" :key="todo.id">
        {{ todo.text }}
      </li>
    </ul>
  </div>
</template>
vue 复制代码
<script lang="ts" setup>
    import { reactive } from 'vue'
    const user = reactive({
        name: '张三',
        age: 25,
        address: {
            city: '北京',
            street: '朝阳区'
        }
    })
    const todos = reactive([
        { id: 1, text: '学习 Vue 3' },
        { id: 2, text: '掌握响应式' }
    ])
    const incrementAge = () => {
    user.age++
    }

    const updateCity = () => {
    user.address.city = '上海'}
     // 添加新任务
    todos.push({
        id: todos.length + 1,
        text: `访问${user.address.city}`
    })

</script>

学习过vue2的同学对于v-for肯定不陌生的,它就是一个循环,只是它比JavaScript中的for...of和for...in更加智能,它可以根据类型自动选择最佳的便利策略,其中:key="todo.id用于给每个渲染的元素提供一个唯一的标识符。是v-bind:key的简写

  • 对于多层的嵌套对象也是响应式的;
js 复制代码
<h2>{{ test.x.y.c }}</h2>
<script lang="ts" setup>
    import { reactive } from 'vue'
    const user = reactive({
        name: '张三',
        age: 25,
        address: {
            city: '北京',
            street: '朝阳区'
        }
    })
    const todos = reactive([
        { id: 1, text: '学习 Vue 3' },
        { id: 2, text: '掌握响应式' }
    ])
     const test = reactive({x:{y:{c:666}}})
相关推荐
威迪斯特几秒前
Flask:轻量级Web框架的技术本质与工程实践
前端·数据库·后端·python·flask·开发框架·核心架构
Hello.Reader25 分钟前
Flink 文件系统通用配置默认文件系统与连接数限制实战
vue.js·flink·npm
wuhen_n27 分钟前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
大鱼前端28 分钟前
为什么我说CSS-in-JS是前端“最佳”的糟粕设计?
前端
不爱吃糖的程序媛31 分钟前
Capacitor:跨平台Web原生应用开发利器,现已全面适配鸿蒙
前端·华为·harmonyos
AC赳赳老秦33 分钟前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
CHU72903534 分钟前
淘宝扭蛋机抽盒小程序前端功能解析:解锁趣味抽盒新体验
前端·小程序
-凌凌漆-1 小时前
【npm】npm的-D选项介绍
前端·npm·node.js
鹿心肺语1 小时前
前端HTML转PDF的两种主流方案深度解析
前端·javascript
海石1 小时前
去到比北方更北的地方—2025年终总结
前端·ai编程·年终总结