用到搜索难免需要来存搜索记录。这是localStorage的功能就显而易见了
1.存搜索记录
export const saveStorage = (key, data) => { try { var serializedState = JSON.stringify(data);//系列化对象。把对象的类型转换为字符串类型 localStorage.setItem(key, serializedState); } catch (err) { //ignore errors. } }
2.拿搜索记录
export const loadStorage = (key) => { try { const serializedState = localStorage.getItem(key); if (serializedState === null) { return undefined; } return JSON.parse(serializedState);//json字符串转换成对象 } catch (err) { return undefined; } }
实例:
let hisTory = {AE:"三十岁",MY:"55",additional:"事实上",brand:"CJ",car_type:"k22",Value:"无验证",state:"1",stateValue:"未开始"}
saveStorage ('key',hisTory);//存 loadStorage('key');//拿