uniapp 數據緩存


驚鴻一面

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');
    }
});
View Code

 

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
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM