小程序:用Storage存储用户搜索历史

今天在写商品搜索时,要记录用户的搜索历史。用setStorage建立一个数组,每次进入商品搜索页面,加载。
代码如下:
一:一进去先读取history表

onLoad:function(){
    const that=this;
        wx.getStorage({
      key: 'history',
      success: function(res) {
        that.setData({
         recent_key:res.data
        });
      }
     });
  }

二:用户输入完毕,点击搜索时触发这个事件getGoodsList

<view class="search-input">
			<icon class="search-icon" type="search" size="16"></icon>
			<input type="text" placeholder="搜索商品名称" confirm-type='search' value="{{inputValue}}"  bindconfirm="getGoodsList" />
		</view>

三:在这个事件里存储用户输入的商品名,其实还有发起搜索请求的作用,这里不放了

 get_history:function(){
    const that=this;
   wx.getStorage({
      key: 'history',
      success: function(res) {
        that.setData({
         recent_key:res.data
        });
      }
     });

  },
 getGoodsList:function(e){
    //建立历史记录表
    this.setData({
      inputValue:e.detail.value
     })

     const inputValue=this.data.inputValue;
         let history=this.data.recent_key;
         history.push(inputValue)
       
       wx.setStorage({
        key:'history',
        data:history
       })

        this.get_history();//把storage的数据存到data里
     }

四,删除history表

<image src="/images/icon/icon-delete.png" bindtap="delete_recent"></image>
  delete_recent:function(){
    const that=this;
  wx.removeStorage({
  key: 'history',
  success (res) {
    that.setData({
      recent_key:[]
    })
  }
})
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值