W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:清理本地?cái)?shù)據(jù)緩存。
Object object
屬性名 | 類(lèi)型 | 必填 | 默認(rèn)值 | 說(shuō)明 |
---|---|---|---|---|
success |
Function |
否 |
接口調(diào)用成功的回調(diào)函數(shù) |
|
fail |
Function |
否 |
接口調(diào)用失敗的回調(diào)函數(shù) |
|
complete |
Function |
否 |
接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |
<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="請(qǐng)輸入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="請(qǐng)輸入value" />
</view>
<view>
<button bindtap="setStorage" type="primary" hover-stop-propagation="true">存儲(chǔ)數(shù)據(jù)</button>
<button bindtap="getStorage" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">讀取數(shù)據(jù)</button>
<button bindtap="getStorageInfo" type="primary" disabled="{{disabled}}">獲取存儲(chǔ)數(shù)據(jù)信息</button>
<button bindtap="clearStorage" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">清理數(shù)據(jù)</button>
</view>
</view>
</view>
Page({
data: {
key: '示例Key',
value: '示例Value',
disabled: true
},
keyFocus() {
this.setData('key', '');
},
valueFocus() {
this.setData('value', '');
},
keyInput(e) {
this.setData('key', e.detail.value);
},
valueInput(e) {
this.setData('value', e.detail.value);
},
setStorage() {
let key = this.hasKey();
if (!key) {
return;
}
swan.setStorage({
key,
data: this.getData('value'),
success: res => {
this.toast('存儲(chǔ)成功', 'none');
this.setData('disabled', false);
},
fail: err => {
this.toast('存儲(chǔ)數(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對(duì)應(yīng)的值');
}
},
fail: err => {
this.toast('讀取數(shù)據(jù)失敗');
}
});
},
clearStorage() {
swan.clearStorage({
success: res => {
this.toast('后臺(tái)數(shù)據(jù)已清除', 'none');
},
fail: err => {
console.log('clearStorage fail', err);
}
});
},
getStorageInfo() {
swan.getStorageInfo({
success: res => {
swan.showModal({
title: '',
content: JSON.stringify(res)
});
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});
}
});
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
1001 | 執(zhí)行失敗 |
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
401 | 安全校驗(yàn)失敗 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: