#author("2024-04-27T10:36:30+08:00","default:Admin","Admin") #author("2024-04-27T10:36:44+08:00","default:Admin","Admin") [[uni-app]] &color(red){※前提条件:vue3 的uniapp开发}; #contents UniApp提供了两种主要的本地存储方式:uni.setStorage 和 uni.getStorage,以及 uni.removeStorage 用于删除数据。这些方法使用异步方式进行操作。 *存储数据: [#hf5abb88] 使用 uni.setStorage 方法将数据存储到本地存储中。例如,将一个字符串存储到本地存储中: #codeprettify{{ uni.setStorage({ key: 'userInfo', data: 'John Doe', success: () => { console.log('数据存储成功'); }, fail: () => {}, }); }} 这将把名为userInfo的键和值John Doe存储到本地存储中。 &color(red){注意:uniapp有一个bug,在onLoad里面,要先remove再set才能够保存数据}; #codeprettify{{ uni.removeStorageSync('IsNewRegist'); uni.setStorageSync('IsNewRegist', true); } }} * 获取数据: [#s7f265b5] 使用 uni.getStorage 方法来获取存储在本地的数据: #codeprettify{{ uni.getStorage({ key: 'userInfo', success: (res) => { console.log('获取的数据为: ' + res.data); }, fail: () => {}, }); }} 这将从本地存储中获取名为userInfo的数据,并在success回调函数中将其打印出来。 * 删除数据: [#m4ed2590] 使用 uni.removeStorage 方法可以删除本地存储中的数据: #codeprettify{{ uni.removeStorage({ key: 'userInfo', success: (res) => { console.log('数据已成功删除'); }, fail: () => {}, }); }} 这将删除名为userInfo的键和与之关联的数据。 #hr(); Comment: #comment_kcaptcha