swan.removeStorage

解释:从本地缓存中异步移除指定 key。

# 方法参数

Object object

# object参数说明

属性名 类型 必填 默认值 说明
key String 本地缓存中的指定的 key
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例 1: 基础用法

    <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">存储数据</button>
                <button bindtap="getStorage" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">读取数据</button>
                <button bindtap="getStorageInfo" type="primary" disabled="{{disabled}}">获取存储数据信息</button>
                <button bindtap="removeStorage" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">移除第一个key对应的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('存储数据失败');
                }
            });
        },
        getStorage() {
            let key = this.hasKey();
            if (!key) {
                return;
            }
    
            swan.getStorage({
                key,
                success: res => {
                    const data = res.data;
                    if (data) {
                        swan.showModal({
                            title: '数据信息',
                            content: `${key}: ${data}`,
                            showCancel: false
                        });
                    }
                    else {
                        this.toast('找不到key对应的值');
                    }
                },
                fail: err => {
                    this.toast('读取数据失败');
                }
            });
        },
        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);
                }
            });
        },
        removeStorage() {
            swan.getStorageInfo({
                success: res => {
                    swan.removeStorage({
                        key: res.keys[0],
                        success: res => {
                            this.toast('removeStorage success', 'none');
    
                        },
                        fail: err => {
                            swan.showToast({
                                title: '没有key值可移除',
                                icon: 'none'
                            });
                            console.log('removeStorage fail', err);
                        }
                    });
                    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});
        }
    });
    
    

    # 参考示例

    # 参考示例 1: 搜索历史

      Page({
          data: {
              value: '',
              queryArr: []
          },
          valueInput(e) {
              this.setData('value', e.detail.value);
          },
          storageQuery() {
              let value = this.getData('value');
              let queryArr = this.getData('queryArr');
              if (value) {
                  // 若输入的存储数据与之前相同,则去重
                  const index = queryArr.findIndex(item => item === value);
                  if (index > -1) {
                      queryArr.splice(index, 1);
                  }
                  // 添加存储数组中的值
                  queryArr.unshift(value);
                  this.setData({queryArr});
              }
              const storeArr = queryArr.slice(0, 20);
              swan.setStorageSync('queryData', storeArr);
          },
          clearQuery() {
              swan.removeStorageSync('queryData');
              this.setData('queryArr', []);
              console.log('搜索历史清空成功');
          }
      });
      
      

      # 错误码

      # Android

      错误码 说明
      201 解析失败,请检查调起协议是否合法
      1001 执行失败

      # iOS

      错误码 说明
      202 解析失败,请检查参数是否正确