用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();
}

转载于:https://www.cnblogs.com/-EFi/p/7063597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值