溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vant-list上拉加載及下拉刷新問題怎么解決

發(fā)布時(shí)間:2022-04-25 16:21:51 來源:億速云 閱讀:2406 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Vant-list上拉加載及下拉刷新問題怎么解決”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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 //當(dāng)前頁碼
    }
  }

第四步

  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下拉刷新與上拉加載一起使用問題

下拉刷新觸發(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ā)上拉加載。

Vant-list上拉加載及下拉刷新問題怎么解決

“Vant-list上拉加載及下拉刷新問題怎么解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI