vue基于vant實(shí)現(xiàn)上拉加載下拉刷新的示例代碼
前言
普遍存在于各種app中的上拉加載下拉刷新功能大家都不陌生吧,一般來說,在數(shù)據(jù)量比較大的情況下,為了更快的渲染和給用戶更好的觀感體驗(yàn),我們會(huì)將數(shù)據(jù)做分頁處理,讓其批量加載,這樣一來,在渲染速度上,能給用戶一個(gè)比較好的體驗(yàn)效果。話說回來,分頁處理,也就是我們今天要講的上拉加載和下拉刷新。
實(shí)現(xiàn)思路
下拉刷新:
請求接口賦完值后,將接口返回?cái)?shù)據(jù)長度與總條數(shù)進(jìn)行比較控制加載不加載的狀態(tài),在下拉刷新方法中定義起始頁碼為第一頁,調(diào)整加載的狀態(tài)為false,最后調(diào)用請求數(shù)據(jù)的接口方法,做適當(dāng)輕提示即可。
上拉加載:
首先在data中定義一個(gè)新的數(shù)組用于將新加載的數(shù)據(jù)與老的數(shù)據(jù)合并,在上拉加載方法中讓加載的頁碼遞增,然后請求接口進(jìn)行賦值,將接口返回?cái)?shù)據(jù)循環(huán)并將數(shù)據(jù)添加到list數(shù)組中,將返回?cái)?shù)據(jù)長度與總條數(shù)進(jìn)行比較控制加載不加載的狀態(tài)實(shí)現(xiàn)上拉加載。
話不多說,下面進(jìn)入實(shí)例開發(fā)。以下是我為了方便大家理解做的一個(gè)小demo,大家可以根據(jù)相關(guān)代碼及注釋快速上手操作。
<template> <div> <!-- van-pull-refresh:下拉刷新 --> <van-pull-refresh v-model="isLoading" @refresh="onRefresh"> <!-- 上拉加載 --> <van-list v-model="loading" :finished="finished" :immediate-check="false" finished-text="沒有更多了" @load="onLoad" :offset="20"> <div class="outerBox" v-for="(item,index) in list" :key="index"> <div>公司名稱:{{item.gsmc}}</div> <div>行業(yè)類別:{{item.hylb}}</div> <div>進(jìn)車限值:{{item.jcxz}}</div> </div> </van-list> </van-pull-refresh> </div> </template> <script> import {getPassAnalysisList} from '@/api/enforcement' //引入接口文件 export default { data() { return { //傳遞給后端的參數(shù) incomePayData: { pageNumber: 1, //請求第幾頁 pageSize: 10, //每頁請求的數(shù)量 }, list: [], //列表數(shù)據(jù) listTwo: [], //用于上拉加載時(shí)將數(shù)據(jù)合并 total: 0, //總頁數(shù) isLoading: false, //下拉刷新 loading: false, //上拉加載 finished: false, //加載與不加載的狀態(tài) } }, mounted() { this.getPassAnalysisList(); //列表數(shù)據(jù)接口 }, methods: { //列表數(shù)據(jù)接口 getPassAnalysisList() { getPassAnalysisList(this.incomePayData).then(res => { if (res.code == '10000') { this.list = res.data.records; //將接口返回賦值data定義的數(shù)組 this.total = res.data.total; //將接口返回的總數(shù)據(jù)條數(shù)賦值data中定義的total if (this.list.length >= this.total) { //將返回?cái)?shù)據(jù)長度與總條數(shù)進(jìn)行比較控制加載不加載的狀態(tài) this.finished = true; } } }) }, //下拉刷新 onRefresh() { this.incomePayData.pageNumber = 1; //起始為第一頁 this.finished = false; //加載與不加載的狀態(tài) setTimeout(() => { this.isLoading = false; //下拉刷新狀態(tài) this.getPassAnalysisList() //刷新成功調(diào)用列表方法 this.$toast('刷新成功'); //輕提示信息 }, 700); }, //上拉加載 onLoad() { this.incomePayData.pageNumber++; //加載時(shí)頁碼遞增 getPassAnalysisList(this.incomePayData).then(res => { if (res.code == '10000') { this.listTwo = res.data.records; //將接口返回賦值data定義的數(shù)組 this.total = res.data.total; //將接口返回的總數(shù)據(jù)條數(shù)賦值data中定義的total this.listTwo.forEach(item => { //循環(huán)返回的數(shù)據(jù)并將數(shù)據(jù)添加到list中 this.list.push(item) }) // 加載狀態(tài)結(jié)束 狀態(tài)更新為false this.loading = false; if (this.list.length >= this.total) { //將返回?cái)?shù)據(jù)長度與總條數(shù)進(jìn)行比較控制加載不加載的狀態(tài) this.finished = true; } } }) }, } } </script> <style scoped> /* 外層盒子的樣式 */ .outerBox { padding: 10px; box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.3), 0px 0px 20px rgba(0, 0, 0, 0.1) inset; margin: 0px 16px 16px 16px; overflow: hidden; } </style>
至此,這個(gè)小功能就實(shí)現(xiàn)啦,下圖是實(shí)現(xiàn)的效果展示,感興趣的話可以自己私下試試喲。
到此這篇關(guān)于vue基于vant實(shí)現(xiàn)上拉加載下拉刷新的文章就介紹到這了,更多相關(guān)vue vant上拉加載下拉刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue+Electron下Vuex的Dispatch沒有效果問題
這篇文章主要介紹了Vue+Electron下Vuex的Dispatch沒有效果的解決方案 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05利用Vue模擬實(shí)現(xiàn)element-ui的分頁器效果
這篇文章主要為大家詳細(xì)介紹了如何利用Vue模擬實(shí)現(xiàn)element-ui的分頁器效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-11-11Vue.js實(shí)現(xiàn)實(shí)例搜索應(yīng)用功能詳細(xì)代碼
本文給大家分享Vue.js實(shí)現(xiàn)實(shí)例搜索應(yīng)用功能詳細(xì)代碼,非常不錯(cuò),感興趣的朋友參考下吧2017-08-08