用Vue写的历史纪录

//历史纪录
//传入的str参数为一个对象;
var str = {
"historyFrom": {
"station": this.fromStationName,
"stationid": this.fromStationId,
"city": this.fromCityName,
"cityid": this.fromCityId,
},
"historyTo": {
"station": this.toStationName,
"stationid": this.toStationId,
"city": this.toCityName,
"cityid": this.toCityId,
}
}
methods:{
history(str){
//历史纪录
let exist = localStorage.getItem("historyAll");
//如果有histroyALL
//还得做一个判断,就是当历史纪录有了同样的纪录的情况
if(exist) {
let history_new = [];
let history_old = JSON.parse(exist);
if(history_old.length > 1) {
let isExist = false; //是否存在于历史纪录当中
let existHistoryIndex; //存在的纪录在历史纪录列表的索引
history_old.forEach((item, index) => {
if(item.historyFrom.station == this.fromStationName &&
item.historyTo.station == this.toStationName) {
isExist = true;
existHistoryIndex = index;
}
});
//如果已存在,就将这个已存在的删除掉
//如果不存在,就删除第二个纪录
if(isExist) {
history_old.splice(existHistoryIndex, 1);
} else {
history_old.splice(1, 1);
}
let history_new = [];
history_new.push(str);
this.historyAll = history_new.concat(history_old); //拼接两个数组为一个数组
} else {
this.historyAll.push(str)
}
localStorage.setItem("historyAll", JSON.stringify(this.historyAll));
}
//如果没有
else {
console.log("bucunzai")
this.historyAll.push(str);
localStorage.setItem("historyAll", JSON.stringify(this.historyAll));
}
},
//页面加载的时候加载local
LocalHistory() {
let histroyall = localStorage.getItem("historyAll");
if(histroyall != null) {
this.historyAll = JSON.parse(histroyall);
}
},
},
created(){
this.LocalHistory();
}

posted @ 2017-06-22 10:29  丿凉意灬o  阅读(1740)  评论(0编辑  收藏  举报