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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM