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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值