咋們還是先看看如何使用vue-scroller:
1、安裝:
使用npm 安裝npm install vue-scroller -d
2、引入:
1 import VueScroller from 'vue-scroller' 2 Vue.use(VueScroller)
(在main.js文件引入)
3、使用:
組件部分
1 <scroller :on-refresh="refresh" :on-infinite="infinite" ref="myscroller" style="padding-bottom:37px;"> 2 <div v-for="(item,index) in notilist" :key="index" @click="click(index)" style="font-size:15px;margin-top:8px;padding:2px 0px;background: #FFFFFF;"> 3 <h1 style="font-size:15px;margin:4px 8px"><b>{{item.title}}<span v-if="item.status=='2'">-(草稿)</span></b></h1> 4 <p style="font-size:13px;margin:0px 8px">{{item.content}}</p> 5 <p style="font-size:13px;margin:0px 8px;color:#999999">{{item.sendTime}}</p> 6 </div> 7 </scroller>
邏輯部分
1 export default { 2 data () { 3 return { 4 noDate:false,//判斷是否加載 5 data:{ 6 keyWord:'', 7 pageIndex:1, 8 pageSize:50, 9 status:'', 10 }, 11 notilist:[] 13 } 14 }, 15 mounted() { 16 this.qryNoticeList(); 17 }, 18 methods: { 19 // 下拉刷新 20 refresh(){ 21 let _this=this; 22 _this.data.pageIndex=1; //重置頁數刷新每次頁數都是第一頁 23 _this.noDate=false; //重置數據判斷 24 _this.qryNoticeList(); 26 }, 27 // 上拉加載 28 infinite(done){ 29 let _this=this; 30 setTimeout(() => { 31 if(_this.noDate){ 32 _this.$refs.myscroller.finishInfinite(true);//finishInfinite
函數為scroller實例的方法,當參數為false時,上拉獲取數據可以重新調用。當參數為true,上拉獲取數據回調函數停止使用,下拉下部不再顯示loading,會顯示‘’暫無更多數據 33 }else{ 34 _this.data.pageIndex++; 35 _this.qryNoticeList(done); 36 37 } 38 }, 1000); 39 }, 40 //獲取重要通知列表 41 qryNoticeList(done){ 42 let _this=this; 43 api.qryNoticeList(_this.data).then((response)=>{ 44 //停止下拉刷新 45 _this.$refs.myscroller.finishPullToRefresh(); 46 if (response.code === 200){ 47 if(typeof (done)=="function"){
done();
} 48 if(response.data.haveNextPage=='0'){ 49 _this.noDate=true; 50 }else{ 51 _this.noDate=false; 52 } 53 // 判斷是下拉刷新還是上拉加載 54 if(_this.data.pageIndex==1){ 55 _this.notilist = response.data.list; 56 }else{ 57 _this.notilist=_this.notilist.concat(response.data.list); 58 } 59 60 }else{ 61 _this.$vux.toast.show({ 62 text: response.msg, 63 width: "14em", 64 type: "cancel" 65 }); 66 return; 67 } 68 }); 69 }
70 } 71 }
4、遇到的坑:
(重點)以上邏輯部分中上拉加載的方法infinite()部分一定先執行setTimeout方法,再進行判斷是否加載,否則會出現多次加載的問題。