1. 用到uniapp的组件
<uni-load-more :status="status" :content-text="contentText"></uni-load-more>
2. 定义变量
data() {
return {
dataList: [],
last_id: '',
reload: true, //判断是否为第一轮加载,以便赋予index值
time: 1, //该值是为了动态更新向服务器传递的参数值
index: 0, //为获取的数据进行分段处理,十条数据一加载
loadingFlag: true, //判断是否继续上拉加载数据
status: 'more', //加载状态
contentText: {
contentdown: '点击记载更多',
contentrefresh: '正在加载...',
contentnomore: '没有更多数据了'
}
};
},
3. 增加事件
onShow() {
this.getDynamicList();
},
// 触底加载更多
onReachBottom() {
if (this.loadingFlag == true) {
this.status = 'more';
this.getDynamicList();
}
},
4. 调用接口获取数据,每次获取五条数据,没下拉加载一次获取五条数据
getDynamicList() {
let that = this;
let params = {
offset: 0,
pageSize: 5
};
if (that.last_id) {
that.status = 'loading';
params.offset = that.time * 5;
that.time++;
}
console.log('params:', params);
newsList(params).then(res => {
if (res.statusCode == 200) {
let data = res.data.rows;
let list = [];
let num = params.pageSize - data.length;
if (that.reload && data.length == 0) {
return false;
} else {
if (!that.reload) {
that.index += 5;
}
if (num !== 0) {
that.status = 'noMore';
that.loadingFlag = false;
}
for (let j = 0; j < data.length; j++) {
list.push(data[j]);
}
that.last_id = list[list.length - 1].id;
that.dataList = that.reload ? list : that.dataList.concat(list);
that.reload = false;
}
}
});
},
亲测有效