img

js历史记录

2021-12-08 0条评论 2.8k次阅读 JavaScript


/** * 最近浏览 * @param item 游戏列表 */ export const newViewEnum = 99; // 游戏栏目"最近浏览"的ID,是为了选中效果 export function setHistoryList(item) { const newViewNum = 8; // 显示历史记录条数 let HistoryList = []; if(window.localStorage.getItem('HistoryList') && window.localStorage.getItem('HistoryList') !== '[""]') { let newHistoryList = JSON.parse(window.localStorage.getItem('HistoryList')); let newArr = HistoryList.concat(newHistoryList) if (newArr.length > 0) { newArr.map((vo,key) => { if(vo.id === item.id) { newArr.splice(key, 1) } }) } newArr.unshift(item) if (newArr.length > newViewNum) { newArr.pop(); } window.localStorage.setItem('HistoryList', JSON.stringify(newArr)) } else { HistoryList.push(item); window.localStorage.setItem('HistoryList', JSON.stringify(HistoryList)) } }

使用方法:

gotoPage(item) { setHistoryList(item); this.$router.push({path: '/goodsList'}); },

💬 COMMENT


🦄 支持markdown语法

👋友