小程序uni中分页咋写(触底加载,分页加载,下拉加载,上拉加载)

我最近写小程序需要处理分页加载之类的把代码优化了一下更清晰,这是自己优化后的把逻辑集中在了方法里面 onReachBottom生命周期里只负责调用

另外也可用于搜索之类的  简简单单三段代码 嘿嘿上代码

data中数据

recordPage: {
					page: 1,//页码
					limit: 10,//每页条数  不用可以去掉
					key: true,//请求开关
					m: '',//搜索条件  不用可以去掉
				},

然后是小程序的生命周期函数

onReachBottom() {
			this.getRecordList()
		},

最后methods

async getRecordList() {
				if (!this.recordPage.key) return
				this.recordPage.key = false//开始请求后关闭开关
				const res = await this.$tokenRequest.post(this.pagesConfig.url, {
					page: this.recordPage.page,
					limit: this.recordPage.limit,
					m: this.recordPage.m,
					type: this.type,
				})

				if (res.list?.length > 0) {
					if (this.recordPage.page == 1) {
						this.recordData = res.list
					} else {
						this.recordData = [...this.recordData, ...res.list]
					}
					this.recordPage.page++//只要有数据页码就++
					this.recordPage.key = true
				} else {
					if(this.recordPage.page == 1) {
						this.recordData = res.list
					}
				}
			},

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值