淺談Vant-list?上拉加載及下拉刷新的問題
更新時間:2022年04月25日 15:02:17 作者:我去沒名字了
這篇文章主要介紹了淺談Vant-list?上拉加載及下拉刷新的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
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="沒有更多了" @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 //當前頁碼
? ? }
? }第四步
? methods: {
? ? onLoad () {
? ? ? this.pageNo++
? ? ? setTimeout(() => {
? ? ? ? if (this.refreshing) {
? ? ? ? ? this.productList = []
? ? ? ? ? this.refreshing = false
? ? ? ? }
? ? ? ? this.loading = false
? ? ? ? const shopId = this.$store.state.user.shopId
? ? ? ? //這里是ajax請求 ?根據(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下拉刷新與上拉加載一起使用問題
下拉刷新觸發(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;
}
}
})
},原因是在于下拉刷新的時候觸發(fā)了上拉加載,所以執(zhí)行了兩次
解決方法是
先將list組價的finished=true,數(shù)據(jù)加載完了在判斷該值應(yīng)該是true還是false,這樣可以避免在下拉刷新的時候觸發(fā)上拉加載。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
深入探索VueJS Scoped CSS 實現(xiàn)原理
這篇文章主要介紹了深入探索VueJS Scoped CSS 實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
解決vue中axios設(shè)置超時(超過5分鐘)沒反應(yīng)的問題
這篇文章主要介紹了解決vue中axios設(shè)置超時(超過5分鐘)沒反應(yīng)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue中使用 pako.js 解密 gzip加密字符串的方法
這篇文章主要介紹了vue項目中 使用 pako.js 解密 gzip加密字符串 的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06

