鸿蒙系统 将数组数据存入storage后取出数据的push和splice方法失效

鸿蒙系统 将数组数据存入storage后取出数据的push和splice方法失效

开发环境 版本号 项目创建方式
Mac mac14.1.1 HBuilderX
(23B81)
HarmonyOS HarmonyOS NEXT Developer Beta2 -

示例代码:

<template>
<view class="container">  
</view>  
</template>  

<script setup>
import {
ref
} from 'vue';
let Cache = {
practice: [{
curId: 0,
node_id: 1,
lists: [{
answer: 'A',
exam_id: 1,
messages: []
},
{
answer: 'B',
exam_id: 2,
messages: []
}
]
}],
practice1: []
}
uni.setStorageSync("exam3Cache", Cache)
setTimeout(() => {
// let exam3Cache = JSON.parse(JSON.stringify(uni.getStorageSync("exam3Cache")))
let exam3Cache = uni.getStorageSync("exam3Cache")
let flog = exam3Cache.practice.some((item, index) => {
if (item.node_id == 1) { //存在这个练习 覆盖
item.lists.push({
answer: 'C',
exam_id: 3,
messages: []
})
console.log("222", item.lists);
}
return item.node_id == 1
})
}, 2000)
</script>  

<style>
.container {
padding: 20px;
font-size: 14px;
line-height: 24px;
}
</style>

操作步骤:

  • 粘贴我的代码,运行鸿蒙复现

预期结果:

item.lists.push({
answer: 'C',
exam_id: 3,
messages: []
}) 成功

实际结果:

item.lists.push({
answer: 'C',
exam_id: 3,
messages: []
}) 失效

bug描述:

鸿蒙系统 将数组数据存入storage,后取出,数据的push和splice方法失效


更多关于鸿蒙系统 将数组数据存入storage后取出数据的push和splice方法失效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

HBuilderX 4.62.2025041603-alpha 已修复。

更多关于鸿蒙系统 将数组数据存入storage后取出数据的push和splice方法失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


如之前沟通,目前需要临时加一个条件编译,在 app-harmony 平台额外 parse 一下,感谢反馈。

加这个parse可以的

因为项目使用缓存的地方太多,如果能修复我就不用改。

这是一个鸿蒙系统下uni-app的Storage数据操作问题。问题原因在于从Storage取出的数据是深拷贝后的对象,失去了原数组的方法。

解决方案:

  1. 使用JSON.parse(JSON.stringify())对取出的数据进行处理:
let exam3Cache = JSON.parse(JSON.stringify(uni.getStorageSync("exam3Cache")))
  1. 或者手动重新构造数组对象:
let exam3Cache = uni.getStorageSync("exam3Cache")
exam3Cache.practice = [...exam3Cache.practice]
回到顶部