开发笔记 : uni-app 小说阅读记录存储

最近用uni-app开发小说APP,需要存储到本地

内容格式我采用的是[ [ ], [ ] ],数组嵌套格式;

在官方文档中找到全端通用globalData机制 :官方文档链接

首先在App.vue 添加 globalData:{ } (globalData 不可变 值的key随便)

<script>
export default {
	globalData: {
		bookReadyHistory:[]
	},
	onLaunch: function() {},
	onShow: function() {},
	onHide: function() {}
};
</script>

在相应页面使用:

let conId  = 0;
let scrollN = 0;
let readHistoryByAppVue = getApp().globalData.bookReadyHistory;

readHistoryByAppVue.forEach(e => {
	if(Number(e[0]) == this.bookId) {
		this.judgeReadHistory = true;
		conId = e[1];
		this.scrollOldAgo = e[2];
		scrollN = e[3];
	}
});

因为我采用数组嵌套存储,所以取出forEach 方法,存入的时候数据放入一个数组arr然后用 push 方法 push 进去;

let arr = [ ];

arr.push(this.bookName);
arr.push(this.bookId);

this.readHistoryByAppVue.push(arr); 

去除重复记录可以先 splice 删除再 push

//	判断存储点是否有小说Id ,重复则删除之前的,再 push 存储

this.readHistoryByAppVue.forEach((item, index, arr) => {	

	if (item[0] == this.bookId) {
		arr.splice(index, 1);
	}
});

你可能感兴趣的:(开发笔记 : uni-app 小说阅读记录存储)