前端如何实现搜索记录展示以及清楚历史搜索记录

<div class="historydata" v-show="input">

            <div>历史搜索</div>

            <ul class="">

              <li v-for="(item, index) in historyList" :key="index" class="text">

                {{ item }}

              </li>

            </ul>

          </div>

 mounted() {

    if (JSON.parse(localStorage.getItem("historyList"))) {

      this.historyList = JSON.parse(localStorage.getItem("historyList"));

    }

  },

点击搜索调下边这个方法

      hostoryfilter(){

  

      

      if (this.input == "") {

        // this.$toast('请输入搜索内容');

        return false;

      } else {

        // 没有搜索记录,把搜索值push进数组首位,存入本地

        if (!this.historyList.includes(this.input)) {

          this.historyList.unshift(this.input);

          localStorage.setItem("historyList", JSON.stringify(this.historyList));

        } else {

          //有搜索记录,删除之前的旧记录,将新搜索值重新push到数组首位

          let i = this.historyList.indexOf(this.input);

          this.historyList.splice(i, 1);

          this.historyList.unshift(this.input);

          localStorage.setItem("historyList", JSON.stringify(this.historyList));

        }

        if(this.historyList.length == 50){

          this.historyList = []

        }

      }

      },

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值