vue 前端本地存储搜索历史记录,删除某条记录,清空记录

vue 前端本地保存搜索历史,localStorage

一、保存历史的数组是由元素组成: [ “000004 SZ”]

  1. 在页面第一次加载的时候,从localStorage中获取历史记录
created() {
		//获取localstorage
        let storage=window.localStorage;
        if(storage.getItem('searchWord') !== null){
        	// _this.historyLis 为渲染到页面
            _this.historyList = JSON.parse(storage.getItem('searchWord'));
        }
      }

2.在搜索功能的方法里调用下面的函数,将当前的记录存储到localStorage中

//item为你当前搜所输入的值
saveHistory(item){
					let storage = window.localStorage;
		            //若无记录历史,直接将新的历史记录设置在localStorage中,若有记录历史,先判断数据是否超过5条,若超过五条				则取最新的五条,去除重复的记录
		            let val = item;
		            if(storage.getItem('searchWord') == null){
		                _this.historyList.unshift(item);
		                storage.setItem('searchWord',JSON.stringify(_this.historyList));
		            }else{
		                if(_this.historyList.indexOf(val) != -1){
		                    _this.historyList.splice(_this.historyList.indexOf(val), 1);
		                    _this.historyList.unshift(val);
		                }else{
		                    _this.historyList.unshift(val);
		                }
		                 if (_this.historyLis.length >= 5) _this.historyLis.pop()
		                storage.setItem('searchWord',JSON.stringify(_this.historyList));
		            }
}

二、保存历史的数组是有对象组成的: [ {number: “00700 HK”, name: “腾讯控股”},{number: “003816 SZ”, name: “中国广核”}]

思路和上方是一样的,只是在处理重复对象的问题的时候有些不一样

//在created()里获取localStorage的值
	if (localStorage.getItem('searchHistory')) {
      this.historyList = JSON.parse(localStorage.getItem('searchHistory'))
    }
//存储当前记录
 saveHistory(name, number) {
      {
        let temp = []
        if (localStorage.getItem('searchHistory')) {
          temp = JSON.parse(localStorage.getItem('searchHistory'))
          if (temp.length >= 5) temp.pop()
          let sameIndex = temp.findIndex((item, index) => {
            return item.number === number
          })
          if (sameIndex >= 0) temp.splice(sameIndex, 1)
        }
        temp.unshift({ number, name})
        localStorage.setItem('searchHistory', JSON.stringify(temp))
        this.historyList = JSON.parse(localStorage.getItem('searchHistory'))
      }
    }

3.删除某条历史记录

deleteHistory (index) {
      const temp = this.historyList
      temp.splice(index, 1)
      localStorage.setItem('searchHistory', JSON.stringify(temp))
      this.historyList = JSON.parse(localStorage.getItem('searchHistory'))
    },

4.清空历史记录

clearHistory(){
            localStorage.clear();
  }
Vue2框架中,实现搜索框的历史记录可以使用localStorage存储搜索记录,并在页面中展示出来。 下面是一个简单的实现代码: ```html <!-- 搜索框 --> <input type="text" v-model="keyword" @keyup.enter="search" /> <!-- 历史记录 --> <div v-if="history.length > 0"> <h3>搜索历史</h3> <ul> <li v-for="(item, index) in history" :key="index" @click="selectHistory(item)"> {{ item }} </li> </ul> </div> ``` ```javascript export default { data() { return { keyword: '', history: [] } }, mounted() { // 从localStorage中读取历史记录 this.history = JSON.parse(localStorage.getItem('searchHistory')) || [] }, methods: { search() { // 搜索处理 // ... // 存储搜索记录到localStorage中 if (this.keyword.trim() !== '') { this.history.unshift(this.keyword) localStorage.setItem('searchHistory', JSON.stringify(this.history)) } }, selectHistory(item) { // 点击历史记录处理 this.keyword = item this.search() } } } ``` 以上代码中,使用了v-model指令将搜索框的值与Vue实例的`keyword`属性进行绑定,使用了`@keyup.enter`事件监听回车键,触发`search`方法进行搜索处理。在搜索处理中,将搜索关键字存储到历史记录中,并使用`localStorage`进行本地存储。在页面中展示历史记录时,使用`v-for`指令遍历历史记录数组,使用`@click`事件监听点击事件,触发`selectHistory`方法进行搜索处理。 需要注意的是,以上代码只是一个简单的实现示例,实际应用中还需要进行一些优化,比如设置历史记录的最大数量、去重等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值