淺談Vant-list?上拉加載及下拉刷新的問(wèn)題
Vant-list 上拉加載及下拉刷新
第一步引入
import { Notify, Dialog, Image, List, PullRefresh } from 'vant'
import Vue from 'vue'
Vue.use(Image).use(List).use(PullRefresh)第二步
<van-pull-refresh v-model="refreshing" @refresh="onRefresh"> ? ? ? <van-list v-model="loading" :finished="finished" finished-text="沒(méi)有更多了" @load="onLoad"> ? ? ? ?? ?<!-- 這里根據(jù)自己需要展示數(shù)據(jù) --> ? ? ? ?</van-list> ? ? </van-pull-refresh>
第三步
?data () {
? ? return {
? ? ? productList: [], //異步查詢數(shù)據(jù)
? ? ? loading: false, //自定義底部加載中提示
? ? ? finished: false,//自定義加載完成后的提示文案
? ? ? refreshing: false,//清空列表數(shù)據(jù)
? ? ? pageNo: 0 //當(dāng)前頁(yè)碼
? ? }
? }第四步
? methods: {
? ? onLoad () {
? ? ? this.pageNo++
? ? ? setTimeout(() => {
? ? ? ? if (this.refreshing) {
? ? ? ? ? this.productList = []
? ? ? ? ? this.refreshing = false
? ? ? ? }
? ? ? ? this.loading = false
? ? ? ? const shopId = this.$store.state.user.shopId
? ? ? ? //這里是ajax請(qǐng)求 ?根據(jù)自己業(yè)務(wù)需求
? ? ? ? pageList({ shopId: shopId, pageNo: this.pageNo, pageSize: 2 }).then(res => {
? ? ? ? ? if (this.validResp(res)) {
? ? ? ? ? ? this.total = res.data.pageNo
? ? ? ? ? ? this.loading = true
? ? ? ? ? ? this.productList.push(...res.data.dataList)
? ? ? ? ? }
? ? ? ? ? if (this.productList.length >= parseInt(res.data.pageNo)) {
? ? ? ? ? ? this.finished = true
? ? ? ? ? }
? ? ? ? })
? ? ? }, 1000)
? ? },
? ? onRefresh () {
? ? ? this.finished = false
? ? ? this.loading = true
? ? ? this.pageNo = 0
? ? ? this.onLoad()
? ? }
? ? }vant下拉刷新與上拉加載一起使用問(wèn)題
下拉刷新觸發(fā)兩次 list與pull
//下拉刷新
onRefresh() {
this.list = [];
this.curPage = 1;
this.finished = true;
this.getData();
},
getData() {
this.isLoading = false;
getList({
curPage: this.curPage,
pageSize: this.pageSize
}).then((res) => {
this.listLoading = false;
if (res.code == 200) {
this.list = this.list.concat(res.data.list);
this.curPage = res.data.nextPage;
if (this.list.length >= res.data.total) {
this.finished = true;
}else {
this.finished = false;
}
}
})
},原因是在于下拉刷新的時(shí)候觸發(fā)了上拉加載,所以執(zhí)行了兩次
解決方法是
先將list組價(jià)的finished=true,數(shù)據(jù)加載完了在判斷該值應(yīng)該是true還是false,這樣可以避免在下拉刷新的時(shí)候觸發(fā)上拉加載。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)GitHub的第三方授權(quán)方法示例
本文主要介紹了vue實(shí)現(xiàn)GitHub的第三方授權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Vue3+ElementPlus封裝圖片空間組件的門面實(shí)例
圖片空間是用于管理上傳圖片的工具,可以讓用戶方便地存儲(chǔ)、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過(guò)圖片空間組件,用戶能直接在其他模塊(例如商品圖片)中選擇所需圖片2024-09-09
vue 基于abstract 路由模式 實(shí)現(xiàn)頁(yè)面內(nèi)嵌的示例代碼
這篇文章主要介紹了vue 基于abstract 路由模式 實(shí)現(xiàn)頁(yè)面內(nèi)嵌的示例代碼,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
Vue中動(dòng)態(tài)權(quán)限到按鈕的完整實(shí)現(xiàn)方案詳解
這篇文章主要為大家詳細(xì)介紹了Vue如何在現(xiàn)有方案的基礎(chǔ)上加入對(duì)路由的增、刪、改、查權(quán)限控制,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
Vue3中同時(shí)定義多個(gè)插槽的實(shí)現(xiàn)示例
本文主要介紹了Vue3中同時(shí)定義多個(gè)插槽的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
vue如何通過(guò)$router.push傳參數(shù)
這篇文章主要介紹了vue如何通過(guò)$router.push傳參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

