vue3 解决reactive数组对象属性更新问题

本文探讨了在 Vue3 setup 语法中遇到的对象数组响应式更新问题。当直接修改数组内对象的属性时,视图无法正确更新。解决方法包括对新增对象使用 `reactive` 包裹,或者直接通过数组索引访问并修改属性。这两个方案都能确保数据变化被 Vue3 检测到,从而触发视图更新。
摘要由CSDN通过智能技术生成

vue3 setup中使用对象数组

const state = reactive<DataProps>({
  fileList:[]
  })

如果更新数组中的对象属性会发现数据不会更新,比如

const newImgFile: ImgFile = {
  path:result,
   status: 'selected',
   file: files[0]
 }
 state.fileList.push(newImgFile)
 newImgFile.path = '123';
解决办法,外面包一层reactive
 const newImgFile: ImgFile = reactive({
   path:result,
    status: 'selected',
    file: files[0]
  })
 state.fileList.push(newImgFile);
 newImgFile.path = '123'; //再修改可以正常更新
或者
 const newImgFile: ImgFile = {
   path:result,
    status: 'selected',
    file: files[0]
  }
 state.fileList.push(newImgFile);
 state.fileList[state.fileList.length-1].path='123'  //再修改可以正常更新
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值