uniapp------添加历史搜索及去重

效果如图:

 <view class="search">
       <input type="text" placeholder="搜索您需要的内容" :value="keyword" @input="input" @confirm="search"/>
       <image src="../../static/sousuo.png" class="icon-search"></image>
</view>
<h2>搜索历史</h2>
<view class="souList" v-if="word_list.length>0">
     <text class="sou" v-for="i in word_list" @click="tapTab(i)" :class="tabIndex == i ?'on':''">
        {{i}}
     </text>
</view>

 

<script>
  export default {
    data() {
      return {
        keyword:'',
		word_list:[],
      }
    },
	onLoad(e) {
      this.openHistorySearch()
	},
    methods: {
      openHistorySearch() {
       this.word_list = uni.getStorageSync('word_list')||[];
      },
      search(res) {
        if(this.keyword==""){
          return false;
        }else{
          if(this.word_list.length<8){
             var newArr = this.word_list.findIndex(item => {
                return item === this.keyword;
             })
             if(newArr != -1){
               this.word_list.splice(newArr,1)
               this.word_list.unshift(this.keyword)
             }else{
                this.word_list.unshift(this.keyword)
             }
          }else{
            this.word_list.pop()
            this.word_list.unshift(this.keyword)
          }
          uni.setStorageSync('word_list',this.word_list)
        }
       
      },
      
    }
		
  }
</script>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值