localstorage本地存储数据实现最近浏览获取
开发思路:
一、存入
在需要记录的地方调用setInLocalStorage方法
方法中需要实现的需求:
1、将localstorage中的数据取出
2、判断存储中是否有数据,如果没有则将新的数据直接存入
3、如果有进行判断,判断要存入的数据是否重复
4、有重复删除之前存入的重复数据
5、应用unshift将新的数据存在最顶部
6、将数组存入localstorage中
setInLocalStorage(nodeData){
var storageBeforePush = JSON.parse(window.localStorage.getItem("recently"))
if(storageBeforePush!=null){
var key = nodeData.name
for(let i=0;i<storageBeforePush.length;i++){
if(key==storageBeforePush[i].name ){
storageBeforePush.splice(i, 1)
}
}
this.storageHistroyList = storageBeforePush
this.storageHistroyList.unshift(nodeData)
}else{
this.storageHistroyList.unshift(nodeData)
}
var storageHistroyList = this.storageHistroyList
window.localStorage.setItem("recently",JSON.stringify(storageHistroyList))
},
二、取出
在取出localstorage中数据时,getItem取出,遍历应用。
renderProcessContentRecently() {
var thizz = this
var storageHistoryList = JSON.parse(window.localStorage.getItem("recently"))
if(storageHistoryList!=null){
for(let i=0;i<storageHistoryList.length;i++){
var list = storageHistoryList[i]
thizz.recentlyListTable.push(list)
}
}else{
thizz.recentlyListTable=null
}
}