Home > Web Front-end > uni-app > body text

How to implement lazy loading in uniapp

coldplay.xixi
Release: 2023-01-13 00:44:15
Original
10019 people have browsed it

How to implement lazy loading in uniapp: You can use the life cycle function [onReachBottom()] to perform the loading operation when the page scrolls to the bottom. The code is [onReachBottom: function()].

How to implement lazy loading in uniapp

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, thinkpad t480 computer.

Recommended (free): uni-app development tutorial

Uniapp’s method of implementing lazy loading:

In uni-app, when we need to lazy load the requested data, we can use the onReachBottom() life cycle function to scroll the page to the bottom. when, perform the loading operation.

The p here is the page number parameter, each time it is loaded, per 1.

onLoad() {
// ajax请求
this.ajaxCode(this.per)
},
onReachBottom: function() {
// 下拉懒加载
++this.per;
uni.request({
url: 'https://www.zrzj.com/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.zrzj.com/api/index/homePage',
method: 'get',
data: {
p: per
},
success: (res) => {
var _data = res.data.result
this.products = _data;
}
})
}
}
Copy after login

Related free learning recommendations: Programming video

The above is the detailed content of How to implement lazy loading in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template