驚鴻一面
uni.getStorage(OBJECT)
從本地緩存中異步獲取指定 key 對應的內容。
OBJECT 參數說明
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
success | Function | 是 | 接口調用的回調函數,res = {data: key對應的內容} |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success 返回參數說明
參數 | 類型 | 說明 |
---|---|---|
data | Any | key 對應的內容 |
示例
uni.getStorage({ key: 'storage_key', success: function (res) { console.log(res.data); } });
uni.getStorageSync(KEY)
從本地緩存中同步獲取指定 key 對應的內容。
參數說明
參數 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
uni.getStorage(OBJECT)
從本地緩存中異步獲取指定 key 對應的內容。
OBJECT 參數說明
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
success | Function | 是 | 接口調用的回調函數,res = {data: key對應的內容} |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success 返回參數說明
參數 | 類型 | 說明 |
---|---|---|
data | Any | key 對應的內容 |
示例
uni.getStorage({ key: 'storage_key', success: function (res) { console.log(res.data); } });
uni.getStorageSync(KEY)
從本地緩存中同步獲取指定 key 對應的內容。
參數說明
參數 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
示例
try { const value = uni.getStorageSync('storage_key'); if (value) { console.log(value); } } catch (e) { // error }
uni.getStorageInfo(OBJECT)
異步獲取當前 storage 的相關信息。
平台差異說明
App | H5 | 微信小程序 | 支付寶小程序 | 百度小程序 |
---|---|---|---|---|
HBuilderX 2.0.3+ | √ | √ | √ | √ |
OBJECT 參數說明
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
success | Function | 是 | 接口調用的回調函數,詳見返回參數說明 |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success 返回參數說明
參數 | 類型 | 說明 |
---|---|---|
keys | Array<String> | 當前 storage 中所有的 key |
currentSize | Number | 當前占用的空間大小, 單位:kb |
limitSize | Number | 限制的空間大小, 單位:kb |
示例
uni.getStorageInfo({
success: function (res) {
console.log(res.keys);
console.log(res.currentSize);
console.log(res.limitSize);
}
});
uni.getStorageInfoSync()
同步獲取當前 storage 的相關信息。
平台差異說明
App | H5 | 微信小程序 | 支付寶小程序 | 百度小程序 |
---|---|---|---|---|
HBuilderX 2.0.3+ | √ | √ | √ | √ |
示例
try { const res = uni.getStorageInfoSync(); console.log(res.keys); console.log(res.currentSize); console.log(res.limitSize); } catch (e) { // error }
uni.removeStorage(OBJECT)
從本地緩存中異步移除指定 key。
OBJECT 參數說明
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
success | Function | 是 | 接口調用的回調函數 |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
示例

uni.removeStorage({ key: 'storage_key', success: function (res) { console.log('success'); } });
uni.removeStorageSync(KEY)
從本地緩存中同步移除指定 key。
參數說明
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地緩存中的指定的 key |
示例
uni.removeStorage({ key: 'storage_key', success: function (res) { console.log('success'); } });
torage()
清理本地數據緩存。
示例
uni.clearStorage();
uni.clearStorageSync()
同步清理本地數據緩存。
示例
try { uni.clearStorageSync(); } catch (e) { // error }