在项目中引入vue-infinite-scroll
npm install vue-infinite-scroll --save
引入插件
import infiniteScroll from "vue-infinite-scroll"
导入指令
export default {
name: "ComFlowList",
directives: {infiniteScroll},
data(){}
}
定义插件和数据列表所需用到的数据状态属性,如下:
data(){
return {
dataList: [],
dataPageNum: 1, //列表数据加载页码
dataPageSize: 20, //列表数据加载每页条数
dataLoadStop: false, //列表数据加载停止状态
dataLoadNomore: false //列表数据加载是否完成
}
}
methods:{
/**
* 加载列表数据
*/
loadDataList(){
//锁定数据加载状态
this.dataLoadStop = true;
let requestUrl = this.dataListIntUrl
this.dataListLoadApi(requestUrl,{
pid: this.pid,
cid: this.cid,
mid: this.mid,
pagesize: this.dataPageSize,
pagenum: this.dataPageNum
})
.then( function(response) {
let queryResult = response.data;
let datas = response.data.list;
this.dataList = this.dataList.concat(datas);
if(queryResult.hasnextpage == 1){
this.dataPageNum += 1
this.dataLoadStop = false
}else{
this.dataNomore = true
}
}.bind(this))
.catch(function (error) { // 请求失败处理
console.log(error);
this.dataLoadStop = false
}.bind(this));
},
}
<template>
<div v-infinite-scroll="loadDataList"
infinite-scroll-disabled="dataLoadStop"
infinite-scroll-distance="50">
<div v-for="(item, idx) in dataList" >
<div class="comdata"><span>{{item.name}}</span></div>
</div>
<div class="list-load-end">
<span>{{dataNomore ? "--我也是有底线的--" : "加载中..."}}</span>
</div>
</div>
</template>