1.首先将上拉加载封装成组件
<template>
<view>
<!-- 上拉加载 -->
<view class="flex align-center justify-center py-3 font">
<text class="font text-light-muted">
{{loadmore}}
</text>
</view>
</view>
</template>
<script>
export default{
props:{
loadmore:String
}
}
</script>
<style>
</style>
我们配置props
接受加载提升文字
在index.vue导入上拉加载组件
//结构
<!-- 滑块视图 -->
<swiper :style="'height:'+scrollH+'px;'" :duration="500" :current="tabIndex" @change="onChangeTab">
<swiper-item v-for="items,indexs in newslist" :key="indexs">
<scroll-view @scrolltolower="loadmore(indexs)" scroll-y="true" :style="'height:'+scrollH+'px;'">
<!-- 判断数据长度 -->
<template v-if="items.list.length>0">
<block v-for="(item,index) in items.list" :key="index">
<!-- 图文列表 -->
<commonList :item="item" :index="index" @follow="follow" @doSupport="doSupport">
</commonList>
<!-- 全局分割线 -->
<divider></divider>
</block>
<!-- 上拉加载 -->
<load-more :loadmore="items.loadmore"></load-more>
</template>
<!-- 没有数据显示 -->
<template v-else>
<nothing-data></nothing-data>
</template>
</scroll-view>
</swiper-item>
</swiper>
<script>
import demo from '@/newsdata.js'//导入新闻数据
import commonList from '@/components/common/common-list.vue'
import loadMore from '@/components/common/load-more.vue'//上拉加载组件
export default {
components:{
commonList,loadMore
},
....
</script>
我们在scroll-view
添加了scrolltolower
事件,当滚动到页面底部时触发loadmore函数,并传入当前数据索引,这个索引用于加载当前栏目的内容
官方文档:组件->视图容器->scroll-view
loadmore(indexs){
//模拟数据请求
let item = this.newslist[indexs]
//修改当前列表状态
//验证是否处于可加载状态(2s之后才进行加载)
if(item.loadmore!=='上拉加载更多'){
return;
}
this.newslist[indexs].loadmore='加载中...'
setTimeout(()=>{
//加载数据
item.list = [...item.list,...item .list]
//恢复数据加载状态
this.newslist[indexs].loadmore='上拉加载更多'
},2000)
}