返回博客

Vue3 响应式数据重新赋值问题详解

本文探讨 Vue3 中使用 reactive 进行响应式数据重新赋值的几种方法,包括 Object.assign, ref, reactive 以及 toRefs 的使用,并分析各自的优缺点。

Mt.r
|

Object.assign

这个最方便

let obj = reactive({ a: {} });
Object.assign(obj,{a:2, b:3})

直接用 ref

let list = ref([{ id: 1, name: 'Andy' }]);
const checkBtn = () => {
  list.value = [{ id: 1, name: 'Andy' }];
  list.value.push({ id: 2, name: 'Lucy' });
};

直接用 reactive

let obj = reactive({ a: {} });

obj.a = { ...obj.a, ...newdata };

toRefs

看文档:https://vuejs.org/api/reactivity-utilities.html#torefs

参考文章