小程序学习之数据缓存API(四)数据的删除

1.异步删除数据

使用wx.removeStorage(OBJECT)从本地缓存中异步删除指定KEY名称和对应的值,参数说明如下:

示例代码格式如下:

其中,引号内的key可替换成实际用到的KEY名称,且success()函数中res包含的内容为{errMsg:"removeStorage:ok"}

2.同步删除数据

使用wx.removeStorageSync(KEY)从本地缓存中同步删除指定KEY名称和对应的值,参数说明如下:

示例代码格式如下:

简单应用示例:

<!--wxml-->
<view class="title">数据存储的简单应用</view>
<view class="demo-box">
  <view class="title">wx.removeStorage(OBJECT)异步删除</view>
    <input name="key" placeholder="请输入KEY名称" bindinput="keyInput"></input>
    <button type="primary" bindtap="removeStorage">数据异步删除</button>
  </view>
<view class="demo-box">
  <view class="title">wx.removeStorageSync(OBJECT)同步存储</view>
    <input name="key1" placeholder="请输入KEY名称" bindinput="keyInput1"></input>
    <button type="primary" bindtap="removeStorageSync">数据同步删除</button>
</view>

//js
Page({
  keyInput:function(e){
    this.setData({
      key:e.detail.value
    })
  },
  removeStorage:function(){
    let key = this.data.key;
    if(key.length==0){
      wx.showToast({
        title: 'KEY不能为空',
        icon: 'none'
      })
    }else{
      wx.removeStorage({
        key: key,
        success: function(res) {
          wx.showToast({
            title: '删除完毕!',
            icon: 'none'
          })
        }
      })
    }
  },
  keyInput1: function (e) {
    this.setData({
      key1: e.detail.value
    })
  },
  removeStorageSync:function(){
    let key = this.data.key1;
    if (key.length == 0) {
      wx.showToast({
        title: 'KEY不能为空',
        icon: 'none'
      })
    } else {
      wx.removeStorageSync(key);
      wx.showToast({
        title: '删除完毕!',
        icon: 'none'
      })
    }
  }
})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值