百度智能小程序 清理本地?cái)?shù)據(jù)緩存

2020-09-05 14:15 更新

swan.clearStorage

解釋:清理本地?cái)?shù)據(jù)緩存。

方法參數(shù)

Object object

object參數(shù)說(shuō)明

屬性名 類(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í)行)

示例 

在開(kāi)發(fā)者工具中打開(kāi)


圖片示例



代碼示例

<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ò)誤碼

Android

錯(cuò)誤碼說(shuō)明

1001

執(zhí)行失敗

iOS

錯(cuò)誤碼說(shuō)明

401

安全校驗(yàn)失敗


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)