uni-app 數據緩存


uni.setStorage(OBJECT)

將數據存儲在本地緩存中指定的key中,會覆蓋掉原來該key對應的內容,這是一個異步接口。

OBJECT參數說明:

參數名 類型 必填 說明
key String 本地緩存中的指定的key
data Object/String 需要存儲的內容
success Function 接口調用成功的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)
setStorage:function(res){
                uni.setStorage({
                    key: 'storage_key',
                    data: 'hello',
                    success: function(){
                        console.log('success');
                    }
                })
            }

uni.setStorageSync(KEY,DATA)

將data存儲在本地緩存中指定的key中,會覆蓋掉原來該key對應的內容,這是一個同步接口。

參數說明:

參數 類型 必填 說明
key Strig 本地緩存中的指定的key
data Object/String 需要存儲的內容

同步方法需要使用 try{}catch(e){}

            try{
                    uni.setStorageSync('storage_key','hello');
                }catch(e){
                    
                }

uni.getStorage(OBJECT)

從本地緩存中異步獲取指定key對應的內容

OBJECT參數說明

參數名 類型 必填 說明
key String 本地緩存中的指定的key
success Function 接口調用的回調函數,res={data:key對應的內容}
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)
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){
                    
                }

uni.getStorageInfo(OBJECT)

異步獲取當前storage的相關信息。

OBJECT參數說明:

參數名 類型 必填 說明
success Function 接口調用的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)

 


免責聲明!

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



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