Vue 3 中 ref 和 reactive 在真实项目里到底怎么选?
用 Vue 3 写业务的时候,`ref` 和 `reactive` 几乎每天都会碰到。很多人刚上手时习惯用 `reactive` 包一个对象,觉得这样模板里不用写 `.value` 很省事;也有人从头到尾只用 `ref`,因为解构、传参、组合函数返回值都更统一。真正放到表单、列表、弹窗、分页这些场景里,两者的差异就会冒出来:`reactive` 解构后失去响应性、整体替换对象时视图不更新、`ref` 在模板里自动解包但在组合函数中要用 `.value`、`watch` 监听对象属性时需要小心深层监听。这篇文章从实际代码出发,把 `ref` 和 `reactive` 的底层机制、适用边界和常见坑讲清楚,重点放在如何根据数据形态和复用方式做选择,而不是背 API 定义。 ## 先看底层:它们都是怎么变成响应式的 Vue 3 的响应式核心是 `Proxy`。`reactive` 接收一个对象,返回它的 Proxy,读写属性时触发依赖收集和派发更新。`ref` 接收任意值,内部如果发现是对象,会用 `reactive` 包一层,但它本身返回的是带 `.value` 的对象,这个 `.value` 的 getter/setter 由 `RefImpl` 实现。 关键差别在于:`reactive` 的响应性绑定在返回的 Proxy 上,原始对象和后续解构出来的属性都不是响应式的;`ref` 的响应性绑定在 ref 实例上,只要引用没丢,`.value` 读写都能被追踪。 ```ts import { reactive, ref, watchEffect } from 'vue' const state = reactive({ count: 0 }) const countRef = ref(0) watchEffect(() => { console.log('reactive:', state.count) }) watchEffect(() => { console.log('ref:', countRef.value) }) state.count++ countRef.value++ ``` 这里两次修改都会触发对应副作用,但注意下面这种写法就会断掉: ```ts let { count } = reactive({ count: 0 }) count++ // 不会触发视图更新 ``` 因为解构拿到的 `count` 只是一个普通 number,Proxy 的 get 拦截只在访问 `state.count` 时发生。 ## ref 在模板和组合函数里的表现 `ref` 在模板中会自动解包,所以写起来和普通变量差不多: ```vue {{ loading ? '加载中' : '刷新' }} {{ item.name }} ``` 组合函数里返回 `ref` 也很自然,调用方拿到后可以用 `.value` 读写,也可以配合 `toRefs` 解构对象。 ```ts export function usePagination() { const page = ref(1) const pageSize = ref(20) const total = ref(0) function next() { page.value++ } return { page, pageSize, total, next } } ``` 调用方: ```ts const { page, next } = usePagination() next() console.log(page.value) ``` 这种模式在团队协作里更稳定,因为返回值结构清晰,类型推导也直接。 ## reactive 适合什么场景 `reactive` 更适合一个内部使用的、结构固定的状态对象,尤其是当你不打算整体替换它、也不需要把它解构出去的时候。比如一个页面的查询条件: ```ts const query = reactive({ keyword: '', status: '', startDate: '', endDate: '' }) function reset() { query.keyword = '' query.status = '' query.startDate = '' query.endDate = '' } ``` 这里每次都是改属性,不会重新赋值整个 `query`,所以 `reactive` 完全够用,模板里也不用写 `.value`。 但一旦出现下面这些操作,就要考虑换成 `ref`: ```ts // 整体替换,reactive 会丢失响应性 let query = reactive({ keyword: '' }) query = reactive({ keyword: 'vue' }) // 模板绑定的还是旧的 Proxy // 解构后使用 const { keyword } = query // keyword 不是响应式的 ``` 用 `ref` 就没有这个问题: ```ts const query = ref({ keyword: '' }) query.value = { keyword: 'vue' } // 正常更新 ``` ## watch 监听时的差异 监听 `ref` 对象时,默认只监听到 `.value` 引用的变化,不会深度遍历内部属性: ```ts const form = ref({ name: '', age: 0 }) watch(form, () => { // 只有 form.value 被整体替换时触发 }) ``` 想监听内部属性变化,要加 `deep`: ```ts watch(form, () => {}, { deep: true }) ``` 监听 `reactive` 对象时,Vue 会自动开启深层监听,因为访问的就是 Proxy 本身: ```ts const form = reactive({ name: '', age: 0 }) watch(form, () => { // 任意属性变化都会触发 }) ``` 但这也意味着性能开销更大,尤其是表单字段多、嵌套深的时候。很多线上问题不是响应式没生效,而是深层监听触发了太多不必要的计算。 ## 模板 ref 和响应式数据 ref 不是一回事 这里容易混:`ref` 既用来声明响应式数据,也用来拿 DOM 或组件实例。 ```vue ``` `inputRef` 初始是 `null`,挂载后变成真实 DOM。它和 `keyword` 都是 `ref`,但用途完全不同。命名上建议区分开,比如 `xxxRef` 表示模板引用,`xxx` 表示数据。 ## 在 TypeScript 里的类型推导 `ref` 的类型推导比较直接: ```ts const count = ref(0) // Ref const user = ref(null) // Ref ``` `reactive` 会丢失一些泛型信息,尤其是当对象结构复杂或使用了 `Partial`、`Record` 时: ```ts interface User { name: string age: number } const user = reactive({ name: '', age: 0 }) // user 的类型是 UnwrapNestedRefs,大部分场景够用 // 但如果要整体替换,reactive 就不合适 ``` 组合函数返回值如果希望调用方能解构且保持响应性,用 `ref` 或 `toRefs` 更稳: ```ts export function useUser() { const user = reactive({ name: '', age: 0 }) return toRefs(user) } ``` 调用方: ```ts const { name, age } = useUser() name.value = 'vue' ``` ## 我自己的选择习惯 简单值用 `ref`,比如 `loading`、`visible`、`page`、`keyword`。 对象如果只是内部改属性、不整体替换、不解构,用 `reactive` 写起来少一层 `.value`,读代码时也更像普通对象。 对象如果需要在组合函数之间传递、需要整体替换、需要解构,或者要作为 `v-model` 的绑定目标,优先用 `ref`。 列表数据、接口返回结果、弹窗表单对象,我基本都用 `ref`,因为后端返回的数据经常整体替换,用 `reactive` 很容易出现视图不更新的问题。 一个容易忽略的点:`reactive` 对象在模板里通过 `v-model` 绑定到子组件时,如果子组件 `emit` 的是新对象,父组件直接赋值会断开响应性,这时候要么用 `ref`,要么在父组件里逐属性赋值。 ```vue ``` 父组件如果写 `const form = reactive({ name: '' })`,然后 ``,子组件 emit 新对象后,`form` 仍然指向旧 Proxy。改成 `const form = ref({ name: '' })` 就不会有这个问题。 ## 不要为了统一而统一 团队里有人规定“全部用 ref”,也有人规定“对象一律 reactive”。这两种规定都会在某些场景下别扭。更实际的做法是:默认用 `ref` 保证响应性和类型推导的稳定性,遇到明确只在内聚状态对象上改属性的场景,再用 `reactive` 减少 `.value` 噪音。代码是给人看的,选择标准应该是“这个数据会不会被整体替换、会不会被解构传出去”,而不是哪个 API 更短。 推荐查看天气:http://www.362tq.com/jianxingan/40tian.html 推荐查看天气:http://www.362tq.com/yongfeng/ 推荐查看天气:http://www.362tq.com/yongfeng/zuotian.html 推荐查看天气:http://www.362tq.com/yongfeng/7tian.html 推荐查看天气:http://www.362tq.com/yongfeng/10tian.html 推荐查看天气:http://www.362tq.com/yongfeng/15tian.html 推荐查看天气:http://www.362tq.com/yongfeng/40tian.html 推荐查看天气:http://www.362tq.com/taihe/ 推荐查看天气:http://www.362tq.com/taihe/zuotian.html 推荐查看天气:http://www.362tq.com/taihe/7tian.html 推荐查看天气:http://www.362tq.com/taihe/10tian.html 推荐查看天气:http://www.362tq.com/taihe/15tian.html 推荐查看天气:http://www.362tq.com/taihe/40tian.html 推荐查看天气:http://www.362tq.com/suichuan/ 推荐查看天气:http://www.362tq.com/suichuan/zuotian.html 推荐查看天气:http://www.362tq.com/suichuan/7tian.html 推荐查看天气:http://www.362tq.com/suichuan/10tian.html 推荐查看天气:http://www.362tq.com/suichuan/15tian.html 推荐查看天气:http://www.362tq.com/suichuan/40tian.html 推荐查看天气:http://www.362tq.com/wanan/ 推荐查看天气:http://www.362tq.com/wanan/zuotian.html 推荐查看天气:http://www.362tq.com/wanan/7tian.html 推荐查看天气:http://www.362tq.com/wanan/10tian.html 推荐查看天气:http://www.362tq.com/wanan/15tian.html 推荐查看天气:http://www.362tq.com/wanan/40tian.html 推荐查看天气:http://www.362tq.com/anfu/ 推荐查看天气:http://www.362tq.com/anfu/zuotian.html 推荐查看天气:http://www.362tq.com/anfu/7tian.html 推荐查看天气:http://www.362tq.com/anfu/10tian.html 推荐查看天气:http://www.362tq.com/anfu/15tian.html 推荐查看天气:http://www.362tq.com/anfu/40tian.html 推荐查看天气:http://www.362tq.com/jianyongxin/ 推荐查看天气:http://www.362tq.com/jianyongxin/zuotian.html 推荐查看天气:http://www.362tq.com/jianyongxin/7tian.html 推荐查看天气:http://www.362tq.com/jianyongxin/10tian.html 推荐查看天气:http://www.362tq.com/jianyongxin/15tian.html 推荐查看天气:http://www.362tq.com/jianyongxin/40tian.html 推荐查看天气:http://www.362tq.com/jinggangshan/ 推荐查看天气:http://www.362tq.com/jinggangshan/zuotian.html 推荐查看天气:http://www.362tq.com/jinggangshan/7tian.html 推荐查看天气:http://www.362tq.com/jinggangshan/10tian.html 推荐查看天气:http://www.362tq.com/jinggangshan/15tian.html 推荐查看天气:http://www.362tq.com/jinggangshan/40tian.html 推荐查看天气:http://www.362tq.com/yichun/ 推荐查看天气:http://www.362tq.com/yichun/zuotian.html 推荐查看天气:http://www.362tq.com/yichun/7tian.html 推荐查看天气:http://www.362tq.com/yichun/10tian.html 推荐查看天气:http://www.362tq.com/yichun/15tian.html 推荐查看天气:http://www.362tq.com/yichun/40tian.html 推荐查看天气:http://www.362tq.com/fengxin/ 推荐查看天气:http://www.362tq.com/fengxin/zuotian.html 推荐查看天气:http://www.362tq.com/fengxin/7tian.html 推荐查看天气:http://www.362tq.com/fengxin/10tian.html 推荐查看天气:http://www.362tq.com/fengxin/15tian.html 推荐查看天气:http://www.362tq.com/fengxin/40tian.html 推荐查看天气:http://www.362tq.com/wanzai/ 推荐查看天气:http://www.362tq.com/wanzai/zuotian.html 推荐查看天气:http://www.362tq.com/wanzai/7tian.html 推荐查看天气:http://www.362tq.com/wanzai/10tian.html 推荐查看天气:http://www.362tq.com/wanzai/15tian.html 推荐查看天气:http://www.362tq.com/wanzai/40tian.html 推荐查看天气:http://www.362tq.com/shanggao/ 推荐查看天气:http://www.362tq.com/shanggao/zuotian.html 推荐查看天气:http://www.362tq.com/shanggao/7tian.html 推荐查看天气:http://www.362tq.com/shanggao/10tian.html 推荐查看天气:http://www.362tq.com/shanggao/15tian.html 推荐查看天气:http://www.362tq.com/shanggao/40tian.html 推荐查看天气:http://www.362tq.com/yifeng/ 推荐查看天气:http://www.362tq.com/yifeng/zuotian.html 推荐查看天气:http://www.362tq.com/yifeng/7tian.html 推荐查看天气:http://www.362tq.com/yifeng/10tian.html 推荐查看天气:http://www.362tq.com/yifeng/15tian.html 推荐查看天气:http://www.362tq.com/yifeng/40tian.html 推荐查看天气:http://www.362tq.com/jingan/ 推荐查看天气:http://www.362tq.com/jingan/zuotian.html 推荐查看天气:http://www.362tq.com/jingan/7tian.html 推荐查看天气:http://www.362tq.com/jingan/10tian.html 推荐查看天气:http://www.362tq.com/jingan/15tian.html 推荐查看天气:http://www.362tq.com/jingan/40tian.html 推荐查看天气:http://www.362tq.com/tonggu/ 推荐查看天气:http://www.362tq.com/tonggu/zuotian.html 推荐查看天气:http://www.362tq.com/tonggu/7tian.html 推荐查看天气:http://www.362tq.com/tonggu/10tian.html 推荐查看天气:http://www.362tq.com/tonggu/15tian.html 推荐查看天气:http://www.362tq.com/tonggu/40tian.html 推荐查看天气:http://www.362tq.com/zhangshu/ 推荐查看天气:http://www.362tq.com/zhangshu/zuotian.html 推荐查看天气:http://www.362tq.com/zhangshu/7tian.html 推荐查看天气:http://www.362tq.com/zhangshu/10tian.html 推荐查看天气:http://www.362tq.com/zhangshu/15tian.html 推荐查看天气:http://www.362tq.com/zhangshu/40tian.html 推荐查看天气:http://www.362tq.com/gaoan/ 推荐查看天气:http://www.362tq.com/gaoan/zuotian.html 推荐查看天气:http://www.362tq.com/gaoan/7tian.html 推荐查看天气:http://www.362tq.com/gaoan/10tian.html 推荐查看天气:http://www.362tq.com/gaoan/15tian.html 推荐查看天气:http://www.362tq.com/gaoan/40tian.html 推荐查看天气:http://www.362tq.com/fuzhoushi/ 推荐查看天气:http://www.362tq.com/fuzhoushi/zuotian.html 推荐查看天气:http://www.362tq.com/fuzhoushi/7tian.html 推荐查看天气:http://www.362tq.com/fuzhoushi/10tian.html 推荐查看天气:http://www.362tq.com/fuzhoushi/15tian.html 推荐查看天气:http://www.362tq.com/fuzhoushi/40tian.html 推荐查看天气:http://www.362tq.com/linchuan/ 推荐查看天气:http://www.362tq.com/linchuan/zuotian.html 推荐查看天气:http://www.362tq.com/linchuan/7tian.html 推荐查看天气:http://www.362tq.com/linchuan/10tian.html 推荐查看天气:http://www.362tq.com/linchuan/15tian.html 推荐查看天气:http://www.362tq.com/linchuan/40tian.html 推荐查看天气:http://www.362tq.com/nancheng/ 推荐查看天气:http://www.362tq.com/nancheng/zuotian.html 推荐查看天气:http://www.362tq.com/nancheng/7tian.html 推荐查看天气:http://www.362tq.com/nancheng/10tian.html 推荐查看天气:http://www.362tq.com/nancheng/15tian.html 推荐查看天气:http://www.362tq.com/nancheng/40tian.html 推荐查看天气:http://www.362tq.com/fuzhoulichuan/ 推荐查看天气:http://www.362tq.com/fuzhoulichuan/zuotian.html 推荐查看天气:http://www.362tq.com/fuzhoulichuan/7tian.html 推荐查看天气:http://www.362tq.com/fuzhoulichuan/10tian.html 推荐查看天气:http://www.362tq.com/fuzhoulichuan/15tian.html 推荐查看天气:http://www.362tq.com/fuzhoulichuan/40tian.html 推荐查看天气:http://www.362tq.com/nanfeng/ 推荐查看天气:http://www.362tq.com/nanfeng/zuotian.html 推荐查看天气:http://www.362tq.com/nanfeng/7tian.html 推荐查看天气:http://www.362tq.com/nanfeng/10tian.html 推荐查看天气:http://www.362tq.com/nanfeng/15tian.html 推荐查看天气:http://www.362tq.com/nanfeng/40tian.html 推荐查看天气:http://www.362tq.com/chongren/ 推荐查看天气:http://www.362tq.com/chongren/zuotian.html 推荐查看天气:http://www.362tq.com/chongren/7tian.html 推荐查看天气:http://www.362tq.com/chongren/10tian.html 推荐查看天气:http://www.362tq.com/chongren/15tian.html 推荐查看天气:http://www.362tq.com/chongren/40tian.html 推荐查看天气:http://www.362tq.com/lean/ 推荐查看天气:http://www.362tq.com/lean/zuotian.html 推荐查看天气:http://www.362tq.com/lean/7tian.html 推荐查看天气:http://www.362tq.com/lean/10tian.html 推荐查看天气:http://www.362tq.com/lean/15tian.html 推荐查看天气:http://www.362tq.com/lean/40tian.html 推荐查看天气:http://www.362tq.com/yihuang/ 推荐查看天气:http://www.362tq.com/yihuang/zuotian.html 推荐查看天气:http://www.362tq.com/yihuang/7tian.html 推荐查看天气:http://www.362tq.com/yihuang/10tian.html 推荐查看天气:http://www.362tq.com/yihuang/15tian.html 推荐查看天气:http://www.362tq.com/yihuang/40tian.html 推荐查看天气:http://www.362tq.com/jinxi/ 推荐查看天气:http://www.362tq.com/jinxi/zuotian.html 推荐查看天气:http://www.362tq.com/jinxi/7tian.html 推荐查看天气:http://www.362tq.com/jinxi/10tian.html 推荐查看天气:http://www.362tq.com/jinxi/15tian.html 推荐查看天气:http://www.362tq.com/jinxi/40tian.html 推荐查看天气:http://www.362tq.com/zixi/ 推荐查看天气:http://www.362tq.com/zixi/zuotian.html 推荐查看天气:http://www.362tq.com/zixi/7tian.html 推荐查看天气:http://www.362tq.com/zixi/10tian.html 推荐查看天气:http://www.362tq.com/zixi/15tian.html 推荐查看天气:http://www.362tq.com/zixi/40tian.html 推荐查看天气:http://www.362tq.com/dongxiang/ 推荐查看天气:http://www.362tq.com/dongxiang/zuotian.html 推荐查看天气:http://www.362tq.com/dongxiang/7tian.html 推荐查看天气:http://www.362tq.com/dongxiang/10tian.html 推荐查看天气:http://www.362tq.com/dongxiang/15tian.html 推荐查看天气:http://www.362tq.com/dongxiang/40tian.html 推荐查看天气:http://www.362tq.com/guangchang/ 推荐查看天气:http://www.362tq.com/guangchang/zuotian.html 推荐查看天气:http://www.362tq.com/guangchang/7tian.html 推荐查看天气:http://www.362tq.com/guangchang/10tian.html 推荐查看天气:http://www.362tq.com/guangchang/15tian.html 推荐查看天气:http://www.362tq.com/guangchang/40tian.html 推荐查看天气:http://www.362tq.com/shangrao/ 推荐查看天气:http://www.362tq.com/shangrao/zuotian.html 推荐查看天气:http://www.362tq.com/shangrao/7tian.html 推荐查看天气:http://www.362tq.com/shangrao/10tian.html 推荐查看天气:http://www.362tq.com/shangrao/15tian.html 推荐查看天气:http://www.362tq.com/shangrao/40tian.html 推荐查看天气:http://www.362tq.com/shangraoxian/ 推荐查看天气:http://www.362tq.com/shangraoxian/zuotian.html 推荐查看天气:http://www.362tq.com/shangraoxian/7tian.html 推荐查看天气:http://www.362tq.com/shangraoxian/10tian.html 推荐查看天气:http://www.362tq.com/shangraoxian/15tian.html 推荐查看天气:http://www.362tq.com/shangraoxian/40tian.html 推荐查看天气:http://www.362tq.com/guangfeng/ 推荐查看天气:http://www.362tq.com/guangfeng/zuotian.html 推荐查看天气:http://www.362tq.com/guangfeng/7tian.html 推荐查看天气:http://www.362tq.com/guangfeng/10tian.html 推荐查看天气:http://www.362tq.com/guangfeng/15tian.html 推荐查看天气:http://www.362tq.com/guangfeng/40tian.html 推荐查看天气:http://www.362tq.com/yushan/ 推荐查看天气:http://www.362tq.com/yushan/zuotian.html 推荐查看天气:http://www.362tq.com/yushan/7tian.html 推荐查看天气:http://www.362tq.com/yushan/10tian.html 推荐查看天气:http://www.362tq.com/yushan/15tian.html 推荐查看天气:http://www.362tq.com/yushan/40tian.html 推荐查看天气:http://www.362tq.com/qianshan/ 推荐查看天气:http://www.362tq.com/qianshan/zuotian.html 推荐查看天气:http://www.362tq.com/qianshan/7tian.html 推荐查看天气:http://www.362tq.com/qianshan/10tian.html 推荐查看天气:http://www.362tq.com/qianshan/15tian.html 推荐查看天气:http://www.362tq.com/qianshan/40tian.html 推荐查看天气:http://www.362tq.com/hengfeng/