W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
解釋:從本地緩存中同步移除指定 key。
String keykey參數(shù)說明 :本地緩存中的指定的 key。
<view class="wrap">
<view class="card-area">
<view class="list-area border-bottom">
<label class="list-item-key-4">key</label>
<input class="list-item-value" bindfocus="keyFocus" bindinput="keyInput" type="text" value="{{key}}" placeholder="請輸入key" />
</view>
<view class="list-area border-bottom">
<label class="list-item-key-4">value</label>
<input class="list-item-value" bindfocus="valueFocus" bindinput="valueInput" type="text" value="{{value}}" placeholder="請輸入value" />
</view>
<view>
<button bindtap="setStorage" type="primary" hover-stop-propagation="true">存儲數(shù)據(jù)</button>
<button bindtap="getStorage" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">讀取數(shù)據(jù)</button>
<button bindtap="getStorageInfo" type="primary" disabled="{{disabled}}">獲取存儲數(shù)據(jù)信息</button>
<button bindtap="removeStorageSync" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">移除第一個key對應(yīng)的value值</button>
</view>
</view>
</view>
Page({
data: {
key: '示例Key',
value: '示例Value',
disabled: true
},
keyInput(e) {
this.setData('key', e.detail.value);
},
valueInput(e) {
this.setData('value', e.detail.value);
},
valueFocus() {
this.setData('value', '');
},
keyFocus() {
this.setData('key', '');
},
setStorage() {
let key = this.hasKey();
if (!key) {
return;
}
swan.setStorage({
key,
data: this.getData('value'),
success: res => {
this.toast('存儲成功', 'none');
this.setData('disabled', false);
},
fail: err => {
this.toast('存儲數(shù)據(jù)失敗');
}
});
},
getStorage() {
let key = this.hasKey();
if (!key) {
return;
}
swan.getStorage({
key,
success: res => {
const data = res.data;
if (data) {
swan.showModal({
title: '數(shù)據(jù)信息',
content: `${key}: ${data}`,
showCancel: false
});
} else {
this.toast('找不到key對應(yīng)的值');
}
},
fail: err => {
this.toast('讀取數(shù)據(jù)失敗');
}
});
},
getStorageInfo() {
swan.getStorageInfo({
success: res => {
swan.showModal({
title: '',
content: JSON.stringify(res)
})
this.setData({keysList: res.keys});
console.log('getStorageInfo success', res);
},
fail: err => {
console.log('getStorageInfo fail', err);
}
});
},
removeStorageSync() {
swan.getStorageInfo({
success: res => {
let result = swan.removeStorageSync(res.keys[0]);
// 基礎(chǔ)庫 3.140.1 之前,無法判斷接口是否調(diào)用失敗
// 基礎(chǔ)庫 3.140.1 及以后,通過 instanceof 來判斷是否未傳必傳值`key`或接口是否調(diào)用失敗
if (!(result instanceof Error)) {
this.toast('removeStorageSync success', 'none');
}
else {
this.toast('removeStorageSync fail', 'none');
}
console.log('getStorageInfo success', res);
},
fail: err => {
console.log('getStorageInfo fail', err);
}
});
},
hasKey() {
let key = this.getData('key');
if (key) {
return key;
}
this.toast('key不能為空');
},
toast(title, icon = 'none') {
swan.showToast({title, icon});
}
});
Error
對象,可通過instanceof
來判斷接口是否調(diào)用失敗。錯誤碼 | 說明 |
---|---|
201 |
解析失敗,請檢查調(diào)起協(xié)議是否合法 |
1001 |
執(zhí)行失敗 |
錯誤碼 | 說明 |
---|---|
202 |
解析失敗,請檢查參數(shù)是否正確 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: