uni-app实现懒加载

在uni-app中,当我们需要让请求的数据进行懒加载时,可以使用onReachBottom()这个生命周期函数,让页面滚到到最底部时,进行加载操作。

这里的p是页码参数,每加载一次,per+1.

onLoad() {
	// ajax请求
	this.ajaxCode(this.per)
},
onReachBottom: function() {
			// 下拉懒加载
			++this.per;
			uni.request({
				url: 'https://www.znstny.cn/api/index/homePage',
				method: 'get',
				data: {
					p: this.per
				},
				success: (res) => {
					var next_data = res.data.result
					// 加载新数组
					this.products = this.products.concat(next_data)
				}
			})
		},
		methods: {
			ajaxCode(per) {
				uni.request({
					url: 'https://www.znstny.cn/api/index/homePage',
					method: 'get',
					data: {
						p: per
					},
					success: (res) => {
						var _data = res.data.result
						this.products = _data;
					}
				})
			}
		}

猜你喜欢

转载自blog.csdn.net/weixin_42604536/article/details/87933310