这篇文章将为大家详细讲解有关使用JavaScript怎么实现一个父组件的请求服务,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<template>
<div class = 'car_list' reft='scrollobx' @scroll='scrollready($event)'>
</div>
</template>
<script>
export default {
data() {
return {
lengthThreshold: 50, //当滑动到距离低端50px时,更新数据
timeThreshold: 300,
promise: null
}
},
methods: {
// 滚动加载列表
scrollready() {
if(this.shouldScroll()) {
if (this.promise) {
return;
}
// 进行加载
this.fn();
// 防止多次滑动,频繁请求后台资源
let self = this;
this.promise = setTimeout(() => {
self.promise = null;
}, this.timeThreshold);
}
},
// 调用父组件请求资源服务
fn() {
this.$emit('scrollChange');
},
shouldScroll() {
if(document.scrollTop === 0) {// 滑动距离为0,还没开始滑动
return false;
}<br data-filtered="filtered"> // 列表高度 - 列表可视高度 - 滑动的高度 < 50px , 则加载更多
return this.$refs.scrollbox.scrollHeight - this.$refs.scrollbox.clientHeight - this.$refs.scrollbox.scrollTop < this.lengthThreshold;
},
}
}
</script>
父组件的请求服务方法:
queryCarApplyShareList() {
this.Load = true;
CarResources.methods.queryCarApplyShareList(this.http, this.pageNo, this.pageSize).then((res) => {
if (res && res.status === 200) {
if (this.pageNo === 1) { // 加载第一页数据
this.CarList = res.data || [];
} else { // 加载更多
this.CarList = <strong>this.CarList.concat(res.data);</strong>
}
this.IsLastPage = res.data.length === 0
}
this.Load = false;
}, () => {
this.Load = false;
console.log('接口报错');
});
},
关于使用JavaScript怎么实现一个父组件的请求服务就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。