可以使用同步的方法uni.setStorageSync、uni.getStorageSync、uni.removeStorageSync和uni.clearStorageSync等方法来操作本地存储和缓存。
设置本地存储
uni.setStorageSync('key', 'value');获取本地存储
let value = uni.getStorageSync('key');删除本地存储
uni.removeStorageSync('key');清除所有本地存储
uni.clearStorageSync(); 异步方法uni.setStorage、uni.getStorage、uni.removeStorage和uni.clearStorage,
设置本地存储
uni.setStorage({key: 'storage_key',data: 'hello',success: function () {console.log('success');}
});
获取本地存储
uni.getStorage({key: 'storage_key',success: function (res) {console.log(res.data);}
});另外,这些方法只能存储字符串类型的数据。如果你需要存储其他类型的数据,可以考虑将它们转换为JSON字符串存储。读取时再反序列化回来。