<van-list
v-model="loading"
:finished="finished"
finished-text="没有更多了"
@load="load"
offset="50"
:immediate-check="false"
>
列表
</van-list>
// 初始化分页列表
initList() {
this.currentPage = 1;
this.list = [];
this.loading = false;
this.finished = false;
this.orderList();
},
// 获取订单列表
async orderList() {
let params = {
token: window.sessionStorage.getItem("token") || "",
page: this.currentPage,
size: 10,
order_type: this.$parent.order,
search_words: this.search || "",
};
console.log("参数", params);
params = this.$public170.webEncrypt(JSON.stringify(params));
let { data } = await this.$axios.post(this.$api.orderList, {
params: params,
});
if (data.code != 1000) return this.$public170.dataAlert(data.msg);
let ost = JSON.parse(this.$public170.webDecrypt(data.data));
console.log("获取订单列表返回", ost);
// 解除防抖
this.$emit("update:disbounce", false);
this.list.push(...ost.order_list);
this.total = this.list.length;
this.loading = false;
if (ost.order_list.length <= 0) {
this.finished = true;
} else {
this.finished = false;
}
},
// 全部列表加载更多
load() {
console.log("加载更多");
if (this.list.length !== this.total) return;
this.currentPage = this.currentPage + 1;
this.orderList();
},
问题是这样,这个分页接口后端未返回总页数,需要前端进行处理最后一页的问题;目前是根据返回的数组长度小于等于0来作为临界点。当在第一个列表请求到非第一页(currentPage>1时)的数据,切换到另外的一个列表,虽然会在切换之前进行分页数据初始化,但是在切换后会直接去触发加载下一页事件(load事件),由此,当我再切换回去的时候,直接调用了两次列表数据(第一页和第二页),接口第二页的返回时间比第一页的返回时间要短,在页面渲染数据时,第二页的数据在列表的前面,第二页的数据在列表的后面展示。
解决方案:在数据返回时记录返回的数据长度,在进行触发加载更多时进行限制判断是否请求下一页数据。
未知领域:为什么在页数大于一的时候切换列表会去触发加载更多呢?
更新:找到为什么会出现连续加载多次的原因了,
需要在orderList方法中发起请求前将loading设置为true状态,在请求返回后再关闭为false。