LocalStorage本地存储
localStorage和sessionStorage
前者生命周期是永远,sessionStorage
的生命周期是当前窗口或当前标签页,
两者都能够存储字符串类型的对象,不同浏览器不能共享localStorage
中的信息
parse
JSON
字符串转为对象
stringify
对象转JSON
字符串转
记录搜索历史记录
async beforeMount() {
if (localStorage.historyList != undefined && localStorage.historyList != "") { // parse方法,将JSON字符串转为数组对象
this.historyList = JSON.parse(localStorage.historyList)
this.isShow = true
} else {
this.isShow = false
}
this.defaulttSearchKey = this.$route.query.defaulttSearchKey;
}
eventFn() { //回车事件
this.historyList = this.historyList.reverse(); //数组反转
this.historyList.push(this.keyword); //将新输入的关键词push到数组
this.historyList = this.historyList.reverse(); //再次将数组反转
this.historyList = Array.from(new Set(this.historyList)); //去除重复值
localStorage.historyList = JSON.stringify(this.historyList);//数组转为字符
}
记录歌曲播放时间,进度条
// 进度条
let progress = this.$refs.audio.currentTime / localStorage.duration;
this.$store.commit("setProgress", progress);
progress = parseInt(progress * 100)
this.tempRate = progress;
this.rate = this.tempRate;
// 播放时间
if (this.paused) {
return;
}
if (localStorage.duration != "NAN" && localStorage.duration && !this.duration) {
this.duration = localStorage.duration;
}
// 设置当前播放时间
this.$store.commit('setCurrentTime', this.$refs.audio.currentTime);
let progress = this.currentTime / this.duration;
// 设置进度条
this.$store.commit("setProgress", progress);
无论是历史记录、进度条还是播放时间这里都保存在了store
里,可在全局进行存取。