這篇文章將為大家詳細講解有關怎么在微信小程序中緩存數據,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
微信小程序可以通過wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)對本地緩存進行設置、獲取和清理。本地緩存最大為10MB。
wx.setStorage()---------異步設置緩存
微信官方給出的屬性
OBJECT參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
| data | Object/String | 是 | 需要存儲的內容 |
| success | Function | 否 | 接口調用成功的回調函數 |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
調用方式:
wx.setStorage({
key:"key",
data:"value"
})wx.setStorageSync()---------同步設置緩存
微信官方給出的屬性
參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
| data | Object/String | 是 | 需要存儲的內容 |
調用方式:
try {
wx.setStorageSync('key', 'value')
} catch (e) {
}wx.getStorage()--------異步獲取緩存
微信官方給出的屬性
OBJECT參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
| success | Function | 是 | 接口調用的回調函數,res = {data: key對應的內容} |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數說明:
| 參數 | 類型 | 說明 |
|---|---|---|
| data | String | key對應的內容 |
調用方式:
wx.getStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})wx.getStorageSync()--------同步獲取緩存數據
微信官方給出的屬性說明
參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
調用方式:
try {
var value = wx.getStorageSync('key')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}wx.getStorageInfo()------異步獲取當前緩存的數據
微信官方給出的屬性說明
OBJECT參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| success | Function | 是 | 接口調用的回調函數,詳見返回參數說明 |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數說明:
| 參數 | 類型 | 說明 |
|---|---|---|
| keys | String Array | 當前storage中所有的key |
| currentSize | Number | 當前占用的空間大小, 單位kb |
| limitSize | Number | 限制的空間大小,單位kb |
調用方式:
wx.getStorageInfo({
success: function(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})wx.getStorageInfoSync()-------同步獲取當前緩存數據
emmmmm---微信并沒有給參數說明
調用方式:
try {
var res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
} catch (e) {
// Do something when catch error
}wx.removeStorage()-----異步移除指定的key的緩存數據
微信官方參數說明
OBJECT參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
| success | Function | 是 | 接口調用的回調函數 |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
調用方式:
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})wx.removeStorageSync()------同步移除指定key的緩存數據
微信官方參數說明
參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| key | String | 是 | 本地緩存中的指定的 key |
調用方式:
try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}wx.clearStorage()------異步清理本地緩存
調用方式:
wx.clearStorage()
wx.clearStorageSync()-------同步清理本地緩存
調用方式:
try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}關于怎么在微信小程序中緩存數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。