一、安裝環(huán)境
"vue": "^2.5.2",
"vue-router": "^3.0.1",
二、所需安裝包
"better-scroll": "^1.12.0",
三、所遇問(wèn)題
項(xiàng)目中,數(shù)據(jù)請(qǐng)求接口需要指定 :
- 頁(yè)數(shù)(pageNum)
- 每頁(yè)數(shù)據(jù)數(shù)量(pageSize)
頁(yè)數(shù)默認(rèn)是1,起初我請(qǐng)求pageSize為30,這樣當(dāng)數(shù)據(jù)量超過(guò)30時(shí),最早的、多出的數(shù)據(jù)就無(wú)法顯示。
四、解決辦法
使用 better-scroll 組件 1.12.0版,使用的不是最新版的(因?yàn)樽钚掳娴氖纠臋n一下子沒看懂,咳咳)。
- 首先查看
scroll組件的文件
<template>
<div class="scroll relative" ref="wrapper">
<slot></slot>
</div>
</template>
<script>
import BScroll from 'better-scroll'
export default {
name: 'scroll',
props: {
/**
* 1 滾動(dòng)的時(shí)候會(huì)派發(fā)scroll事件,會(huì)截流。
* 2 滾動(dòng)的時(shí)候?qū)崟r(shí)派發(fā)scroll事件,不會(huì)截流。
* 3 除了實(shí)時(shí)派發(fā)scroll事件,在swipe的情況下仍然能實(shí)時(shí)派發(fā)scroll事件
*/
probeType: {
type: Number,
default: 1
},
/**
* 點(diǎn)擊列表是否派發(fā)click事件
*/
click: {
type: Boolean,
default: true
},
/**
* 是否開啟橫向滾動(dòng)
*/
scrollX: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)滾動(dòng)事件
*/
listenScroll: {
type: Boolean,
default: false
},
/**
* 列表的數(shù)據(jù)
*/
data: {
type: Array,
default: null
},
/**
* 是否派發(fā)滾動(dòng)到底部的事件,用于上拉加載
*/
pullup: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)頂部下拉的事件,用于下拉刷新
*/
pulldown: {
type: Boolean,
default: false
},
/**
* 是否派發(fā)列表滾動(dòng)開始的事件
*/
beforeScroll: {
type: Boolean,
default: false
},
/**
* 當(dāng)數(shù)據(jù)更新后,刷新scroll的延時(shí)。
*/
refreshDelay: {
type: Number,
default: 20
}
},
mounted() {
// 保證在DOM渲染完畢后初始化better-scroll
setTimeout(() => {
this._initScroll()
}, 20)
},
methods: {
_initScroll() {
if (!this.$refs.wrapper) {
return
}
// better-scroll的初始化
this.scroll = new BScroll(this.$refs.wrapper, {
probeType: this.probeType,
click: this.click,
startY: 0,
scrollX: this.scrollX
})
Log('scroll', this.scroll);
// 是否派發(fā)滾動(dòng)事件
if (this.listenScroll) {
this.scroll.on('scroll', (pos) => {
this.$emit('scroll', pos)
})
}
// 是否派發(fā)滾動(dòng)到底部事件,用于上拉加載
if (this.pullup) {
this.scroll.on('scrollEnd', () => {
// 滾動(dòng)到底部
if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
this.$emit('scrollToEnd')
}
})
}
// 是否派發(fā)頂部下拉事件,用于下拉刷新
if (this.pulldown) {
this.scroll.on('touchend', (pos) => {
// 下拉動(dòng)作
if (pos.y > 50) {
this.$emit('pulldown')
}
})
}
// 是否派發(fā)列表滾動(dòng)開始的事件
if (this.beforeScroll) {
this.scroll.on('beforeScrollStart', () => {
this.$emit('beforeScroll')
})
}
},
disable() {
// 代理better-scroll的disable方法
this.scroll && this.scroll.disable()
},
enable() {
// 代理better-scroll的enable方法
this.scroll && this.scroll.enable()
},
refresh() {
// 代理better-scroll的refresh方法
this.scroll && this.scroll.refresh()
},
scrollTo() {
// 代理better-scroll的scrollTo方法
this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
},
scrollToElement() {
// 代理better-scroll的scrollToElement方法
this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
}
},
watch: {
// 監(jiān)聽數(shù)據(jù)的變化,延時(shí)refreshDelay時(shí)間后調(diào)用refresh方法重新計(jì)算,保證滾動(dòng)效果正常
data() {
setTimeout(() => {
this.refresh()
}, this.refreshDelay)
}
}
}
</script>
<style scoped>
</style>
查看 scroll 組件中的方法,找到自己所需要的函數(shù),根據(jù)需求,此時(shí)我們需要的是 pullup函數(shù),所以我們要在使用到scroll組件的地方,傳遞(props) pullup為true,用以激活方法。
props:{
/**
* 是否派發(fā)滾動(dòng)到底部的事件,用于上拉加載
*/
pullup: {
type: Boolean,
default: false
},
}
methods:{
// 是否派發(fā)滾動(dòng)到底部事件,用于上拉加載
if (this.pullup) {
this.scroll.on('scrollEnd', () => {
// 滾動(dòng)到底部
if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
this.$emit('scrollToEnd')
}
})
}
}
在頁(yè)面中使用scroll組件
<template>
<scroll :pullup='true' @scrollToEnd='scrollToEnd' >
<order-item @click="click"
v-for="(item,index) in list" >
</order-item>
</scroll>
</template>
<script>
export default {
data(){
return{
pageNum,
pagesCount //用于判斷頁(yè)面是否全部加載完
}
}
methods{
scrollToEnd(){
if(this.pageNum < this.pageCount){
this.pageNum ++ ; //每次加載下一頁(yè),都要把頁(yè)數(shù)加一
this.getData();
}
},
async getData() {
let {code, msg, list, page} = await this.$http('smarthos.yygh.apiOrderService.ghBespeakList', {
pageNum: this.pageNum,
pageSize: 10
});
code != 0 && (bus.$emit('msg', msg));
if (code == 0) {
this.pageCount = page.pagesCount;
this.list.length == 0 && (this.show = true);
this.list = this.list.concat(list);
}
},
}
}
</script>
最后實(shí)現(xiàn)效果
首次加載,只顯示10條數(shù)據(jù),屏幕滑到底部就觸發(fā)scroll事件,加載第二頁(yè)的10條處數(shù)據(jù)。