vue3中给数组赋值丢失响应式的解决

作者:凡小多 时间:2024-05-22 10:44:41 

vue3给数组赋值丢失响应式的解决

由于vue3使用proxy,对于对象和数组都不能直接整个赋值。

只有push或者根据索引遍历赋值才可以保留reactive数组的响应性

const arr = reactive([]);

const load = () => {
 const res = [2, 3, 4, 5]; //假设请求接口返回的数据
 // 方法1 失败,直接赋值丢失了响应性
 // arr = res;
 // 方法2 这样也是失败
 // arr.concat(res);
 // 方法3 可以,但是很麻烦
 res.forEach(e => {
   arr.push(e);
 });
 // 方法4 可以
 // arr.length = 0 // 清空原数组
 arr.push(...res)
}

或者

const state = reactive({
arr: []
});
...
state.arr = res
...

或者

const state = ref([]);
...
state.value= res
...

例子

<template>
   <div class="content">
...
       <Card title="content组件">
           <button @click.prevent="add">ADD</button>
           <button @click.prevent="pop">POP</button>
           <button @click.prevent="changeList">changeList</button>
           {{ list }}
           <ProInj></ProInj>
       </Card>
   </div>
</template>

<script setup lang="ts">
import { reactive, markRaw, ref, defineAsyncComponent, inject, Ref } from 'vue'
...
let flag = ref(true)

let list = inject<Ref<number[]>>('list',ref(reactive<number[]>([])))
// let list = inject<Ref<number[]>>('list', ref<number[]>([1, 2, 3]))
const add = () => list.push(list!.length + 1)
const pop = () => list.pop()

const changeList = () => {
   flag.value = !flag.value
   if (flag.value) {
       list.length = 0
       list.push(...[9, 5, 4, 1])
   }
   else {
       list.length = 0
       list.push(...[6, 6, 6, 6, 6])
   }
}
...
</script>

<style lang="less" scoped>
...
</style>

效果图:

vue3中给数组赋值丢失响应式的解决

reactive响应式数据赋值丢失响应式问题

reactive响应式数据赋值问题

const ?list = reactive({})

当你接收到接口数据,不要直接赋值 比如 list = data

这样会丢失响应式!

你可以这样做:

?? ?const ?list = reactive({
?? ?arr:[]
})

list.arr = data.arr

或者

将list声明为ref方式

const list = ref([])
list.value = data

这样也不会丢失响应式

原因:reactive声明的响应式对象被list代理  操作代理对象需要有代理对象的前缀,直接覆盖会丢失响应式 

来源:https://blog.csdn.net/wgh4318/article/details/125654801

标签:vue3,数组,赋值,响应式
0
投稿

猜你喜欢

  • python3实现ftp服务功能(客户端)

    2023-05-28 00:36:31
  • 解决FCKEditor在IE10、IE11下的不兼容问题

    2023-05-26 14:01:22
  • js+html5实现半透明遮罩层弹框效果

    2024-05-08 09:33:09
  • Selenium之模拟登录铁路12306的示例代码

    2022-01-22 17:06:27
  • python简单实现获取当前时间

    2022-08-28 16:38:49
  • 利用python和百度地图API实现数据地图标注的方法

    2023-01-30 11:59:43
  • python 函数定位参数+关键字参数+inspect模块

    2023-07-05 23:04:37
  • 如何通过Python3和ssl实现加密通信功能

    2022-04-28 05:55:30
  • Python设计模式之建造者模式实例详解

    2021-07-13 17:37:03
  • ORACLE正则匹配查询LIKE查询多个值检索数据库对象

    2024-01-20 18:11:01
  • python实现读取并显示图片的两种方法

    2023-12-20 12:51:14
  • python实现多线程的方式及多条命令并发执行

    2023-08-09 11:37:20
  • Python常见内置高阶函数即高阶函数用法

    2021-03-19 20:38:07
  • mysql到oracle的移植

    2011-01-29 16:23:00
  • Python+DeOldify实现老照片上色功能

    2021-07-13 00:04:46
  • 解决python路径错误,运行.py文件,找不到路径的问题

    2023-03-13 05:47:33
  • python脚本使用阿里云slb对恶意攻击进行封堵的实现

    2021-11-20 16:32:07
  • Python数据可视化详解

    2021-10-02 19:28:55
  • Pyinstaller加密打包成反编译可执行文件

    2022-06-20 14:23:06
  • python二分法查找算法实现方法【递归与非递归】

    2023-04-17 08:13:47
  • asp之家 网络编程 m.aspxhome.com