Vue?vant-ui框架實(shí)現(xiàn)上拉加載下拉刷新功能
下拉刷新效果:
知識(shí)點(diǎn)速記
基本用法
List通過loading
和finished
兩個(gè)變量控制加載狀態(tài),當(dāng)組件滾動(dòng)到底部時(shí),會(huì)觸發(fā)load
事件并將loading
設(shè)置成true
。此時(shí)可以發(fā)起異步操作并更新數(shù)據(jù),數(shù)據(jù)更新完畢后,將loading
設(shè)置成false
即可。若數(shù)據(jù)已全部加載完畢,則直接將finished
設(shè)置成true
即可。
下拉刷新
List 組件可以與PullRefresh組件結(jié)合使用,實(shí)現(xiàn)下拉刷新的效果。
注意事項(xiàng):
- v-model : 是否處于加載狀態(tài),加載過程中不觸發(fā)
load
事件 - finished: 是否已加載完成,加載完成后不再觸發(fā)
load
事件 - offset : 滾動(dòng)條與底部距離小于 offset 時(shí)觸發(fā)
load
事件 - loading-text加載過程中的提示文字
- finished-text加載完成后的提示文字
List
有以下三種狀態(tài),理解這些狀態(tài)有助于你正確地使用List
組件:
- 非加載中,
loading
為false
,此時(shí)會(huì)根據(jù)列表滾動(dòng)位置判斷是否觸發(fā)load
事件(列表內(nèi)容不足一屏幕時(shí),會(huì)直接觸發(fā)) - 加載中,
loading
為true
,表示正在發(fā)送異步請(qǐng)求,此時(shí)不會(huì)觸發(fā)load
事件 - 加載完成,
finished
為true
,此時(shí)不會(huì)觸發(fā)load
事件
在每次請(qǐng)求完畢后,需要手動(dòng)將loading
設(shè)置為false
,表示加載結(jié)束
事件使用:@load方法
滾動(dòng)條與底部距離小于 offset 時(shí)觸發(fā)
代碼實(shí)現(xiàn)
1.頁(yè)面布局
<template> <div class="myOrder"> <!--公共頭部組件--> <nav-bar-top :titleText="titleName" :arrowTrue="arrowTrue"></nav-bar-top> <!--主體部分--> <div class="tabMain" style="margin-top:0"> <div v-if="noData" class="p30 text-center fontSize30"> <van-empty class="custom-image" :image="zwdd"/> </div> <van-pull-refresh v-model="isLoading" @refresh="onRefresh" class="content" success-text="刷新成功"> <!-- 加載 --> <van-list v-if="contractData.length>0" v-model="loading" :finished="finished" :immediate-check="false" finished-text="我也是有底線的" @load="onLoad" :offset="10"> <van-row class="m30 p30 capaIttem" v-for="item in contractData" :key="item.orderId"> <van-col span="24" class="fontSize30"> <div class="focus-con"> <!-- ellipsis-orderNumber --> <div class="focus-title"> <span class="color3 van-ellipsis">合同編號(hào):{{item.contractNumber}}</span> <!-- <span class="color3 van-ellipsis">{{item.outsideContractId?'訂單':'合同'}}編號(hào):{{item.contractNumber}}</span> --> <!-- <img src="../../assets/index/VIPICO.png" class="vipIcon pl20"> --> </div> <!-- 發(fā)票開具狀態(tài)(0:待申請(qǐng),1:待審核,2:審核未通過,3:審核已通過) --> <div> <van-tag :text-color="item.status=='0'?'#FF9133':item.status=='1'?'#4972FF':item.status=='2'?'#F1403C':'#00CE80'" class="tagStyle"> {{item.status=="0"?"待申請(qǐng)":item.status=="1"?"待審核":item.status=="2"?"審核未通過":"審核通過"}} </van-tag> </div> </div> </van-col> <van-col span="24" class="mt10"> <div class="capa-box"> <div class="focus-title" > <span class="color3 shipName fontSize32">{{item.userName}}</span> </div> </div> <div class="capa-box"> <div class="color6 flex align-items fontSize24 van-ellipsis ellipsis-content"> <img src="../../assets/img/huo.png" class="huoIcon mr5"> {{item.unlimitedLoading&&item.unlimitedLoading=="1"?"隨船裝/":""}} <span class="" v-if="item.cargoWeight">{{item.cargoWeight}}噸/</span> <span class="van-ellipsis">{{item.cargoName?item.cargoName:""}}</span> </div> </div> </van-col> <van-col span="24" class="capa-body"> <div class="capa-con fontSize28"> <div class="content-left"> <img src="../../assets/index/zhuang.png" alt="" class=" mr5"> <span>{{item.loadingPort}}</span> </div> <img src="../../assets/index/arrows.png" class="capa-jt"> <div class="content-right"> <img src="../../assets/index/xie.png" alt="" class=" mr5"> <span>{{item.arrivePort}}</span> </div> </div> <div class="capa-time mt10"> <div class="capa-time-left"> <div class="time-img"> <img src="../../assets/index/time.png" class="capaImg"> <span class="timeTXT fontSize28 pl10">裝貨時(shí)間</span> </div> <div class="timeColor fontSize34 mt10 timeError"> {{item.loadingTime?item.loadingTime:"暫無(wú)"}} </div> </div> <div style="height:50px;border-left:1px solid #E1E1E1"></div> <div class="capa-time-right"> <div class="time-img priceContent"> <img src="../../assets/index/money.png" class="capaImg"> <span class="timeTXT fontSize28 pl10">合同金額</span> </div> <div> <div class="timeColor fontSize34 mt10 floatRight"> <span v-if="item.contractAmount"> {{item.priceType=="0"?item.contractAmount+"元":item.priceType=="1"?item.contractAmount+"元/噸":item.contractAmount+"元"}} </span> <span v-else>暫無(wú)報(bào)價(jià)</span> </div> </div> </div> </div> </van-col> <van-col style="width:100%" class="mt20"> <div class="flex flex-end"> <van-button plain type="info" class="borderRaduis" size="small" @click="handleLook(item)">查看合同</van-button> <van-button v-show="item.status=='2'" plain type="info ml20" class="borderRaduis" size="small" @click="handleFail(item,item.invoiceId)">失敗原因</van-button> <van-button class="ml20 borderRaduis" color="#024ee0" type="info" size="small">{{item.status=='2'?'重新申請(qǐng)':'申請(qǐng)開票'}}</van-button> </div> </van-col> </van-row> </van-list> </van-pull-refresh> </div> </div> </template>
2.樣式
.myOrder{ height: 100vh; overflow-y: auto; } ::v-deep.van-cell { font-size: 14px; } .capaIttem { box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.06); border-radius: 24px; background: #fff; position: relative; } ::v-deep.van-cell { padding: 10px 0; } ::v-deep.van-cell { font-size: 40px; line-height: 40px; font-size: 30px; } ::v-deep.van-field__left-icon .van-icon { font-size: 30px; } ::v-deep.van-tag { font-size: 24px; line-height: 40px; } ::v-deep.van-button--small { height: 60px; font-size: 24px; } ::v-deep.van-tabs--line .van-tabs__wrap { height: 88px; } ::v-deep.van-tab { font-size: 30px; } .searchBtnFixed { position: fixed; top: 0; left: 80px; height: 92px; line-height: 92px; z-index: 1000; width: 86%; overflow: hidden; } ::v-deep.van-search { position: absolute; top: -8px; // padding: 13px; width: 100%; } ::v-deep.van-search__content { background: #eeeeee; border-radius: 16px; padding: 5px 10px; } .focusSearch ::v-deep.van-icon-clear { padding-right: 160px; } // 搜索 .onSearch { position: absolute; right: 50px; top: 28px; font-size: 30px; height: 36px; line-height: 36px; border-left: 1px solid #cccccd; padding-left: 30px; color: #024ee0; } // 新增 .focus-con { display: flex; justify-content: space-between; border-bottom: 1px solid #ebedf0; padding-bottom: 20px; } .capa-title { border-bottom: 1px solid #ebedf0; padding-bottom: 20px; display: flex; align-items: center; } .focus-title { display: flex; align-items: center; justify-content: space-between; font-weight: 600; } .vipIcon { width: 72px; height: 34px; } .capa-box { display: flex; justify-content: space-between; align-items: center; padding-bottom: 10px; } .capa-body { background: #f5f8ff; border-radius: 0.13333rem; padding: 0.33333rem; } .capa-con { display: flex; justify-content: space-between; align-items: center; font-weight: 600; } .content-left, .content-right { display: flex; justify-content: space-between; align-items: center; img { width: 0.5rem; height: 0.5rem; } } .capa-time { display: flex; justify-content: space-between; align-items: center; } .time-img { display: flex; align-items: center; } .capaImg { width: 30px; } .timeColor { font-weight: 700; color: #024ee0; } .order-group { display: flex; align-items: center; } .ellipsis-orderNumber{ span{ display: inline-block; width: 310px; } } .ellipsis-txt { display: inline-block; max-width: 200px; text-align: start; } .shipName { font-weight: 600; display: flex; align-items: center; } .orderNameWidth{ max-width: 220px; } .ellipsis-content{ width: 400px; } .priceContent{ display: flex; justify-content: flex-end; } .floatRight{ float: right; } .borderRaduis { border-radius: 10px; height: 56px; .van-button__text { font-size: 26px; } } .huoIcon{ width: 26px; height: 26px; } .tagStyle{ background-color: #fff !important; } .contactTel { width: 90%; border-radius: 10px; } .popup-header{ height: 100px; text-align: center; color:#fff; background: #024EE0; line-height: 100px; } .contactTel ::v-deep.van-popup__close-icon--top-right{ top:26px !important; color:#fff !important; } .popup-box{ margin:60px 50px 30px; } .popup-btn{ width: 100%; border-radius: 20px; font-size: 36px; } .popupTitle { box-sizing: border-box; padding: 35px 0 61px 0; font-size: 32px; color: #333333; font-weight: 500; } .imgContent { display: flex; align-items: center; justify-content: space-around; flex-wrap: wrap; margin-bottom: 30px; padding: 0 10px; li { width: 125px; height: 125px; margin-bottom: 37px; background-color: black; margin-right: 25px; } li:nth-of-type(4n) { margin-right: 0; } }
3.方法
data數(shù)據(jù)定義:
data() { return { zwdd:require("../../assets/img/zwdd.png"), titleName: "我要開票", arrowTrue: true, page: 1, pageSize: 10, isLoading: false, loading: false, finished: false, noData: false, contractData: [], timer:null, }; },
加載更多方法onLoad:
// 加載更多 onLoad() { this.loading = true; this.timer = setTimeout(() => { this.page++; this.getInvoicingContractListFun(); }, 1000); },
下拉刷新方法onRefresh:下拉刷新調(diào)用接口,并且頁(yè)數(shù)=1
// 刷新 onRefresh() { this.isLoading = true; this.page = 1; if (this.contractData.length == 0) { this.isLoading = false; } this.getInvoicingContractListFun(); },
處理數(shù)據(jù)列表方法getInvoicingContractListFun:
調(diào)用接口,傳遞相應(yīng)的參數(shù),獲取到數(shù)據(jù),去判斷處理數(shù)據(jù)數(shù)據(jù)的加載,數(shù)據(jù)合并操作,利用定義的loading、finished、isLoading控制數(shù)據(jù)上拉數(shù)據(jù)加載。
// 數(shù)據(jù)列表方法 getInvoicingContractListFun() { let params = { carrierUserId: this.id, pageNum: this.page, pageSize: this.pageSize }; getInvoicingContractList(params) .then(res => { if (res.code == 200) { if (this.contractData.length > 0) { this.noData = false; //當(dāng)請(qǐng)求前有數(shù)據(jù)時(shí) 第n次請(qǐng)求 if (this.loading) { // 上拉加載 this.contractData = this.contractData.concat(res.data.records); //上拉加載新數(shù)據(jù)添加到數(shù)組中 this.$nextTick(() => { //在下次 DOM 更新循環(huán)結(jié)束之后執(zhí)行延遲回調(diào) this.loading = false; //關(guān)閉上拉加載中 }); if (res.data.records.length == 0) { //沒有更多數(shù)據(jù) this.finished = true; //上拉加載完畢 } } if (this.isLoading) { //關(guān)閉下拉刷新 this.isLoading = false; //關(guān)閉下拉刷新中 this.contractData = res.data.records; //重新給數(shù)據(jù)賦值 if (this.finished) { //如果上拉加載完畢為true則設(shè)為false。解決上拉加載完畢后再下拉刷新就不會(huì)執(zhí)行上拉加載問題 this.finished = false; } } } else { this.noData = false; this.loading = false; this.isLoading = false; this.finished = false; //當(dāng)請(qǐng)求沒有數(shù)據(jù)時(shí) 第一次請(qǐng)求 if (res.data.records.length == 0) { this.noData = true; } else { this.contractData = res.data.records; } } } else { this.$toast(res.msg); } }) .catch(error => {}); },
created中調(diào)用方法獲取數(shù)據(jù)
created(){ //方法調(diào)用 this.getInvoicingContractListFun(); }
最后在beforeDestroy生命周期清除定時(shí)器
beforeDestroy() { clearTimeout(this.timer); this.timer = null; }
到此這篇關(guān)于Vue vant-ui框架實(shí)現(xiàn)上拉加載下拉刷新功能的文章就介紹到這了,更多相關(guān)Vue vant-ui內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue使用better-scroll實(shí)現(xiàn)下拉刷新、上拉加載
- vue插件mescroll.js實(shí)現(xiàn)移動(dòng)端上拉加載和下拉刷新
- vueScroll實(shí)現(xiàn)移動(dòng)端下拉刷新、上拉加載
- vue實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀑布流 + 下拉刷新 + 上拉加載更多(步驟詳解)
- vue基于vant實(shí)現(xiàn)上拉加載下拉刷新的示例代碼
- vue實(shí)現(xiàn)原生下拉刷新
- Vue?uni-app框架實(shí)現(xiàn)上拉加載下拉刷新功能
- vant/vue實(shí)現(xiàn)小程序下拉刷新功能方法詳解
相關(guān)文章
vue父組件向子組件動(dòng)態(tài)傳值的兩種方法
這篇文章主要介紹了vue父組件向子組件動(dòng)態(tài)傳值的兩種方法 ,需要的朋友可以參考下2017-11-11antd中table展開行默認(rèn)展示,且不需要前邊的加號(hào)操作
這篇文章主要介紹了antd中table展開行默認(rèn)展示,且不需要前邊的加號(hào)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-11-11vue使用SVG實(shí)現(xiàn)圓形進(jìn)度條音樂播放
這篇文章主要為大家詳細(xì)介紹了vue使用SVG實(shí)現(xiàn)圓形進(jìn)度條音樂播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue+axios實(shí)現(xiàn)文件上傳的實(shí)時(shí)進(jìn)度條
最近用vue寫上傳的時(shí)候,遇到一個(gè)需求就是頁(yè)面上展示上傳的進(jìn)度條,之后寫過一次,但是用的是假交互,直接從0-100,今天分享一下用axios自帶的onUploadProgress來(lái)完成這個(gè)小需求,感興趣的朋友可以參考下2024-01-01非Vuex實(shí)現(xiàn)的登錄狀態(tài)判斷封裝實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于非Vuex實(shí)現(xiàn)的登錄狀態(tài)判斷封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02