实现方法:
1.定义 getList 方法,此方法的作用是调用接口获取记录数据,一次获取6条;
当最新获取的记录为空时,将 nextPage 设定为 false
2.在created阶段调用 getList 方法获得默认 tab 的初始数据,在 mounted 阶段初始化默认 tab 的 iscroll
在 onScrollEnd 事件上绑定 getList 方法,触发条件为 nextPage 为 true,目的为当记录已全部加载完后不再调用接口
3.切换 tab 时,销毁另一个 tab 的 iscroll,防止切换 tab 时的弹跳效果;将pageIndex,nextPage,type 等初始化,切换 tab 时重新加载
代码:
data() {
return {
discount: [],//优惠券记录
upload: [],//小票记录
pageIndex: 0,
nextPage: true,
type: 1,
}
},
created() {
this.getList();
},
mounted() {
let that = this;
this.$nextTick(() => {
recordScroll = new iScroll('wrapper_record', {
checkDOMChanges: true,
vScrollbars: false,
hScroll: false,
hideScrollbar: false,
scrollbarClass: 'none',
onScrollEnd: function () {//分页
if (!that.nextPage) return false;
that.getList()
}
});
});
},
methods: {
tab(type) {//切换记录框
let that = this;
this.pageIndex = 0;
this.nextPage = true;
this.discount = [];
this.upload = [];
this.type = type;
this.getList();
switch (type) {
case 1:
uploadScroll.destroy();
this.show = type;
this.discountBtn = this.cdnUrl + 'discount_click.png';
this.uploadBtn = this.cdnUrl + 'upload_unclick.png';
this.$nextTick(() => {
recordScroll = new iScroll('wrapper_record', {
checkDOMChanges: true,
vScrollbars: false,
hScroll: false,
hideScrollbar: false,
scrollbarClass: 'none',
onScrollEnd: function () {//分页
if (!that.nextPage) return false;
that.getList()
}
});
// YH.iscroll('wrapper_record', 'none');
});
break;
case 2:
recordScroll.destroy();
this.show = type;
this.discountBtn = this.cdnUrl + 'discount_unclick.png';
this.uploadBtn = this.cdnUrl + 'upload_click.png';
this.$nextTick(() => {
uploadScroll = new iScroll('wrapper_upload', {
checkDOMChanges: true,
vScrollbars: false,
hScroll: false,
hideScrollbar: false,
scrollbarClass: 'none',
onScrollEnd: function () {//分页
if (!that.nextPage) return false;
that.getList()
}
});
});
break
}
},
async getList() {
let that = this;
that.pageIndex += 1;
let res = await EsHttp.post(Api.Action.GetDrawRecord.url, {
PageIndex: that.pageIndex,
PageSize: 6,
Type: that.type
});
console.log('获取记录', res);
if (res.Success) {
if (res.Data[0]) {
if (that.type == 1) {
that.discount = that.discount.concat(res.Data);
} else if (that.type == 2) {
that.upload = that.upload.concat(res.Data);
}
} else {
that.nextPage = false
}
}
},
},