Vue基础(2)响应式基础

一. reactive()

在 Vue3 中,可以使用 reactive() 创建一个响应式对象或数组:

xml 复制代码
<script setup>
import { reactive } from 'vue'

const state = reactive({ count: 0 })
</script>

<template>
  <button @click="state.count++">
    {{ state.count }}
  </button>
</template>

需要注意的是,reactive() 返回的是一个原始对象的 Proxy,它与原始对象是不相等的。只有代理对象是响应式的,更新原始对象并不会触发更新。

js 复制代码
const row = {}
const proxy = reactive(raw)

console.log( proxy === row)  	// false

reactive() 的局限性:

  • 有限的值类型:reactive() 只能用于对象、数组等对象类型,而不能用于基础数据类型(string、number、boolean)
  • 不能替换整个对象,会导致对初始引用的响应性连接丢失
xml 复制代码
<script setup>
import { reactive } from 'vue'

let state = reactive({ count: 0 })
const increase = () => {
  state = { count: 1 }		// state.count = 1 才能正确修改
}
</script>

<template>
  <button @click="increase">
    {{ state.count }}			<!-- 点击button,始终显示为0 -->
  </button>
</template>
  • 对解构操作不友好:将响应式对象的原始类型属性解构为本地变量时,或者将该属性传递给函数时,会丢失响应性。
xml 复制代码
<script setup>
import { reactive } from 'vue'

let state = reactive({ count: 0 })
let { count } = state

const increase = () => {
  count++
}
</script>

<template>
  <button @click="increase">
    {{ state.count }}		<!-- 点击button,始终显示为0 -->
  </button>
</template>

二. ref()

ref() 将传入的参数包装成一个带有 value 属性的 ref 对象。在模板中使用 ref 时,不需要附加 .value。

xml 复制代码
<script setup>
import { ref } from 'vue'

const count = ref(0)
console.log(count) 		// {value: 0},使用 count.value++
</script>

<template>
  <button @click="count++">
    {{ count }}			<!-- 无需.value -->
  </button>
</template>

ref() 的参数一般时基础数据类型,也可以是对象类型。如果参数是对象类型,系统会自动将 ref 转成 reactive。

xml 复制代码
<script setup>
import { ref } from 'vue'

const state = ref({ count: 0 })
console.log(state.value) 		// Proxy(object) {count: 0}
const increase = () => {
  state.value.count++
}
</script>

<template>
  <button @click="increase">	<!-- state.count++ -->
    {{ state.count }}			<!-- 无需.value -->
  </button>
</template>

一个包含对象的 ref 可以响应式地替换整个对象:

xml 复制代码
<script setup>
import { ref } from 'vue'

const state = ref({ count: 0 })
const increase = () => {
  state.value = { count: 1 }
}
</script>

<template>
  <button @click="increase">	
    {{ state.count }}			<!-- 点击button后由0变为1 -->
  </button>
</template>
相关推荐
积流成海21 分钟前
用代码实现哈希计算器:Web Crypto API 实战
前端·算法·哈希算法·哈希函数·盘子工具站
data analyse 45628 分钟前
服务端访问日志聚合 PV 与 UV 怎么实现?日志采集与口径设计
大数据·前端·数据分析
song50129 分钟前
React Native for OpenHarmony 实战:三方库 react-native-ratings 的鸿蒙化适配指南
javascript·react native·react.js·分类·harmonyos
JavaGuide33 分钟前
DeepSeek Harness 重磅更新,Codex、Claude Code 子智能体进官方插件列表了,还有 Git 工作树和思考翻译!
前端·后端
WeiXin_DZbishe1 小时前
基于微信小程序的本地闲置物品交易平台-计算机毕设 附源码【课程设计】26825
vue.js·spring boot·python·微信小程序·架构·php·课程设计
小羊没烦恼!1 小时前
OS X 上使用.NET开发应用程序
开发语言·前端·数据库·人工智能·c#·策略模式
朱包林1 小时前
iTerm2终端配置指南
linux·javascript·python·云原生·容器·kubernetes·云计算
高级程序源1 小时前
springboot本地生活服务平台33399-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·小程序·flask·课程设计
高级程序源1 小时前
springboot社区旧物回收管理系统25381-计算机课程设计、毕业设计
vue.js·spring boot·python·django·课程设计
高级程序源1 小时前
springboot大学生体测系统72170-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·django·课程设计