1.封裝擴展scroll.vue功能;vue
1 //props傳值
pullup:{ 2 type:Boolean, 3 default:false 4 } 5 6 7 _initScroll(){ 8 if(!this.$refs.wrapper){ 9 return 10 } 11 this.scroll = new BScroll(this.$refs.wrapper,{ 12 probeType: this.probeType, 13 click: this.click 14 }) 15 if(this.listenScroll){//初始化時候判斷是否監聽滾動 16 let _this=this 17 this.scroll.on('scroll',(pos)=>{ 18 _this.$emit('scroll',pos) 19 }) 20 } 21 if(this.pullup){//滾動到底部的時候進行事件監聽,刷新搜索列表//這裏只是派發事件; 22 this.scroll.on('scrollEnd',()=>{ 23 if(this.scroll.y <=(this.scroll.maxScrollY + 50)){ 24 this.$emit('scrollToEnd') 25 } 26 }) 27 } 28 },
2.在suggest中傳值:後端
1 <scroll 2 :pullup="pullup" 3 @scrollToEnd = 'searchMore' 4 >
3.searchMore方法:app
a.data聲明一個標識位hasMore:true;this
b.當發送search()請求,請求後端數據的時候hasMore值爲true,spa
c.封裝_checkMore()方法,當已經顯示條數大於總條數的時候將hasMore:false;code
d.每次search()請求的最後調用_checkMore方法肯定hasMore的值;blog
e.當hasMore:true的時候發送再次觸發searchMore事件事件
1 _checkMore(data){ 2 const song = data.song 3 if(!song.list.length || (song.curnum + song.curPage*perPage)>song.totalnum){//頁碼數*每頁數量大於總條數 4 this.hasMore = false 5 } 6 }, 7 searchMore(){ 8 if( !this.hasMore){ 9 return 10 } 11 this.page++ 12 search(this.query,this.page,this.showSinger,perpage).then((res) =>{ 13 if (res.code === ERR_OK) { 14 this.result = this.result.concat(this._genResult(res.data)) 15 this._checkMore(res.data) 16 } 17 }) 18 },