uni-app 小程序端 uni.setStorageSync 无效

uni-app 小程序端 uni.setStorageSync 无效

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

产品分类:uniapp/小程序/微信

PC开发环境操作系统:Windows

HBuilderX类型:正式

HBuilderX版本号:3.4.7

第三方开发者工具版本号:1.05.2203070

基础库版本号:2.17

项目创建方式:HBuilderX

示例代码:

<template>
首选科目(二选一)
{{item.name}}
再选科目(四选二)
{{item.name}}
确定
确定
</template> <script> export default { data() { return { subjectTypeList: {}, firstSubjectRequirement: [], secondSubjectRequirement: [], firstRequirement: [], secondRequirement: [], } }, computed: { firstRequirementLength() { /* 是否选取完整 */ return this.firstRequirement.length && this.secondRequirement.length==2 } }, onLoad() { let temp = uni.getStorageSync('student-list') || '{}' temp = JSON.parse(temp) let first = new Array() if(temp.firstSubject) { first.push(temp.firstSubject) } this.firstRequirement = first this.secondRequirement = temp.secondSubject || [] this.querySubjectType() }, methods: { toggleRequirement(val, list, max) { let index = list.findIndex(item => item == val) if (index != -1) { list.splice(index,1) return } if (list.length >= max) { // this.secondRequirement = this.secondRequirement.slice(0,1) list.pop() } list.push(val) }, inList(list, val) { let index = list.findIndex(item => item == val) if (index != -1) { return true } return false }, async querySubjectType() { const { data, code } = await this.$api.request({ data: {}, method: 'GET', }) if (code == 200) { this.subjectTypeList = data || {} this.firstSubjectRequirement = this.subjectTypeList.firstSubjectRequirement || [] this.secondSubjectRequirement = this.subjectTypeList.secondSubjectRequirement || [] } }, submit() { console.log('submit') let temp = uni.getStorageSync('student-list') || '{}' temp = JSON.parse(temp) temp.firstSubject = this.firstRequirement[0] temp.secondSubject = this.secondRequirement // #ifdef MP-WEIXIN wx.setStorageSync('student-list',JSON.stringify(temp)) // #endif // #ifndef MP-WEIXIN uni.setStorageSync('student-list',JSON.stringify(temp)) // #endif // uni.navigateTo({ // url:'./page' // }) uni.navigateBack({ delta:1 }) } } </script> <style lang="scss" scoped> .component-subject-score { padding-bottom: 100rpx; .txt-center { text-align: center; } .form { margin: 36rpx 24rpx; .form-item { padding-bottom: 40rpx; .label { font-size: 32rpx; font-weight: bold; line-height: 44rpx; color: #333333; margin-bottom: 24rpx; .tips { font-size: 26rpx; font-weight: bold; line-height: 36rpx; color: #FF6060; } } .input { padding: 20rpx; border-radius: 10rpx; background: #F6F7F9; font-size: 28rpx; line-height: 40rpx; color: #333333;

}
}
.btn-wrap {
margin-top: 20rpx;
.btn {
margin: 0 auto 36rpx;
height: 92rpx;
background: rgba(153, 153, 153, 0.5);
border-radius: 20rpx;
font-size: 28rpx;
line-height: 92rpx;
color: #FFFFFF;
text-align: center;
&.blue {
background: rgba(53, 134, 255, 1);
}
&.orange {
background: rgba(254, 173, 51, 1);
}
}
}
.check-box-group {
display: flex;
flex-direction: row;
flex-wrap: wrap;
.check-box {
margin-right: 36rpx;
margin-bottom: 20rpx;
padding: 20rpx 32rpx;
background: #FFFFFF;
border-radius: 10rpx;
font-size: 28rpx;
line-height: 40rpx;
color: #333333;
&:last-child {
margin-left: 0rpx;
}
&.active {
color: rgba(255, 255, 255, 1);
background: rgba(53, 134, 255, 1);
}
}
}
</style>


更多关于uni-app 小程序端 uni.setStorageSync 无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

请找出来具体原因后提供可复现bug的最小化demo(上传附件),让我们及时定位问题,及时修复。 【bug优先处理规则】https://ask.dcloud.net.cn/article/38139

更多关于uni-app 小程序端 uni.setStorageSync 无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 uni-app 中,uni.setStorageSync 是用于在小程序端同步存储数据的 API。如果你发现 uni.setStorageSync 无效,可能是以下几个原因导致的:

1. 检查小程序环境

  • 确保你是在小程序环境中运行代码。uni.setStorageSync 是小程序端的 API,如果在 H5 或 App 端使用,可能会无效。
  • 你可以通过 uni.getSystemInfoSync().platform 来检查当前运行环境。
const platform = uni.getSystemInfoSync().platform;
console.log(platform); // 输出当前平台,如 'ios', 'android', 'devtools' 等

2. 检查存储的键名和值

  • 确保你传入的键名和值是有效的。键名必须是字符串,值可以是字符串、对象、数组等。
  • 如果值是一个对象,uni.setStorageSync 会自动将其序列化为字符串存储。
uni.setStorageSync('key', 'value'); // 存储字符串
uni.setStorageSync('key', { name: 'John' }); // 存储对象

3. 检查存储空间

  • 小程序的本地存储空间是有限的,通常为 10MB。如果存储的数据超过了这个限制,可能会导致存储失败。
  • 你可以使用 uni.getStorageInfoSync 来查看当前存储空间的使用情况。
const storageInfo = uni.getStorageInfoSync();
console.log(storageInfo); // 输出当前存储信息

4. 检查代码执行顺序

  • 确保 uni.setStorageSync 是在正确的时机执行的。如果代码执行顺序有问题,可能会导致存储操作未生效。
// 确保在需要的时候执行存储操作
function saveData() {
    uni.setStorageSync('key', 'value');
}
saveData();

5. 检查小程序开发者工具

  • 如果你是在小程序开发者工具中测试,有时开发者工具可能会有缓存或 bug,导致存储操作无效。可以尝试清除缓存或重启开发者工具。

6. 检查权限

  • 确保小程序有权限使用本地存储。通常情况下,小程序默认有权限使用本地存储,但如果小程序配置了某些限制,可能会导致存储操作无效。

7. 调试和日志

  • 使用 console.loguni.showToast 来调试代码,确保 uni.setStorageSync 被正确调用。
try {
    uni.setStorageSync('key', 'value');
    console.log('存储成功');
} catch (e) {
    console.error('存储失败', e);
}

8. 检查小程序基础库版本

  • 确保你使用的小程序基础库版本支持 uni.setStorageSync。虽然这个 API 是比较基础的,但在某些非常旧的版本中可能会有兼容性问题。

9. 检查代码逻辑

  • 确保你的代码逻辑没有覆盖或清除了存储的数据。例如,如果你在某个地方调用了 uni.removeStorageSyncuni.clearStorageSync,可能会导致存储的数据被清除。

10. 使用异步 API 作为替代

  • 如果 uni.setStorageSync 仍然无效,可以尝试使用异步的 uni.setStorage 作为替代。
uni.setStorage({
    key: 'key',
    data: 'value',
    success: function () {
        console.log('存储成功');
    },
    fail: function (err) {
        console.error('存储失败', err);
    }
});
回到顶部