vant中l(wèi)ist的使用以及首次加載觸發(fā)兩次解決問題
vant中l(wèi)ist使用及首次加載觸發(fā)兩次
以下是list的基本使用方法,主要原理就是當頁面數據小于offset這個高度的時候,就會觸發(fā)load,在load里面需要調用接口發(fā)送下一頁的數據.所以發(fā)送完畢后需要將設置分頁的屬性加一,并將獲取到的新值push進接收數據的數組里,而不是直接賦值,如果直接賦值那么數組里就只有新值,之前的值就被覆蓋.
調用完以后將loading的樣式關閉,并且判斷數據庫中還有沒有數據,如果沒有,就將finished為true,表示已經加載完畢了.
首次加載觸發(fā)兩次解決問題
1.在mounted或者create調用,原因是有可能在數據沒回來的時候load就監(jiān)測到數據低于高度,也發(fā)送了一次,等到數據回來時已經請求兩次了.所以干脆不需要調用,交給load檢測即可.
2.offset過于高,默認的高度是300,有一次獲取數據一次只獲取5條,雖然覆蓋了頁面高度,但稍作觸碰就會二次發(fā)送.
3.請求的數據過少,請求的數據不足以覆蓋頁面就會二次加載,可以看文檔list第一條示例便是.
<template> <div> <div class="groupBuyingList"> <!-- 加入加載 --> <van-list v-model="loading" :finished="finished" finished-text="沒有更多了" @load="onLoad" :offset='50' > <!-- 每個模塊 --> <div class="activity" v-for="(item, index) in results" :key="index"> <img :src="item.photo" alt="" /> <div class="title">{{ item.cname }}</div> <div class="groupPeople"> <span>{{ item.groupLabel }}</span> </div> <div class="operation"> <div class="money"> <!-- 拼團價 --> <div class="groupMoney"> <span>¥</span>{{ item.groupPrice }} <span>起</span> </div> <!-- 原價 --> <div class="originalCost">¥{{ item.underlinedPrice }}</div> </div> <div class="share" @click="handleGo(item)"> <span> 去開團 </span> </div> </div> </div> </van-list> </div> </div> </template>
<script> import { activityList } from "../../../api/appletGroupBuying/groupBuyingActivity"; export default { data() { return { userInfo: { // 條數 pageNum: 1, }, loading: false, //加載中狀態(tài) finished: false, //是否加載 // 接收列表數據 results: [], // 總條數 rowCount: "", }; }, mounted() { }, methods: { async activityList() { let res = await activityList(this.userInfo);//發(fā)送請求 // console.log(this.results); // 如果沒有數據 if (res.data.ret.results === null) { this.results = []; this.finished = true; // 停止加載 } else { // 總條數 this.rowCount = res.data.ret.rowCount; // 將數據放入數組 this.results.push(...res.data.ret.results); this.loading = false; // 加載狀態(tài)結束 // 如果list長度大于等于總數據條數,數據全部加載完成 //this.finished = true 結束加載狀態(tài) this.results.length >= this.rowCount ? (this.finished = true) : ""; } }, onLoad() { this.activityList(this.userInfo); // 調用上面方法,請求數據 this.userInfo.pageNum++; // 分頁數加一 }, }, }; </script>
<style lang="less" scoped> .groupBuyingList { padding: 20px; background: #f4f4f4; //每個活動 .activity { padding: 30px 35px; margin-bottom: 32px; width: 710px; background: #ffffff; border-radius: 20px; box-sizing: border-box; > img { width: 100%; } // 標題 .title { margin-top: 30px; width: 100%; height: 40px; font-size: 28px; font-weight: bold; line-height: 40px; color: #545353; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } // 拼團人數 .groupPeople { margin-top: 26px; margin-bottom: 14px; display: flex; align-items: center; span { padding: 0 3px 0 3px; border: 2px solid #ff7f00; border-radius: 10px; font-size: 24px; font-weight: 400; line-height: 33px; color: #ff7f00; } } .operation { display: flex; justify-content: space-between; .money { display: flex; // 拼團價 .groupMoney { display: flex; margin-right: 13px; height: 62px; font-size: 44px; font-weight: bold; line-height: 62px; color: #ff8105; span { font-size: 30px; } } // 原價 .originalCost { text-decoration: line-through; width: 119px; height: 40px; font-size: 28px; line-height: 60px; color: #b5b4b1; } } //分享獲客 .share { width: 180px; height: 60px; background: #ff8105; display: flex; align-items: center; border-radius: 20px; span { width: 100%; line-height: 60px; text-align: center; font-size: 29px; font-weight: bold; line-height: 37px; color: #ffffff; } } } } } </style>
vant中l(wèi)ist列表組件使用
List 的運行機制:
List 會監(jiān)聽瀏覽器的滾動事件并計算列表的位置,當列表底部與可視區(qū)域的距離小于offset時,List 會觸發(fā)一次 load 事件。
1. 基礎結構
<van-list? ?? ?v-model="loading" ?? ? ? ? ? ? ? ? ? ?// 是否顯示正在加載狀態(tài) ?? ?:finished="finished" ?? ??? ??? ??? ?// 是否已經加載完成 ?? ?finished-text="沒有更多了"?? ??? ??? ?// 加載完成提示文案 ?? ?@load="onLoad" ?? ??? ??? ??? ??? ??? ?// 滾動條與底部距離offset時觸發(fā)事件 ?? ?offset="300"?? ??? ??? ??? ??? ??? ?// 滾動條與底部距離offset時觸發(fā)@load(默認300) ?? ?:error.sync="error" ?? ??? ??? ??? ?// 是否顯示加載失敗狀態(tài) ?? ?error-text="請求失敗,點擊重新加載"?? ?// 加載失敗提示文案 ?? ?> ?? ? ? ?<van-cell v-for='(item, index) in list' :key="index"> ?// 循環(huán)列表數據 ? ??? ??? ?<div>{{item}}循環(huán)出來的數據<div> ? ?</van-cell> ?</van-list> ? 2.data聲明: data() { ? ? return { ? ? ? loading: false, ? ? ? ? // 是否處在加載狀態(tài) ? ? ? finished: false, ? ? ? ?// 是否已加載完成 ? ? ? error: false, ? ? ? ? ? // 是否加載失敗 ? ? ? list: [], ? ? ? ? ? ? ? // 數據項 ? ? ? page: 1, ? ? ? ? ? ? ? ?// 分頁 ? ? ? page_size: 10 ? ? ? ? ? // 每頁條數 ? ? ? total: 0 ? ? ? ? ? ? ? ?// 數據總條數 ? ? } }
3.methods定義方法
methods: { ? ? onLoad() { ? ? ? // 異步更新數據 ? ? ? // setTimeout 僅做示例,真實場景中一般為 ajax 請求 ? ? ? setTimeout(() => { ? ? ? ? for (let i = 0; i < 10; i++) { ? ? ? ? ? this.list.push(this.list.length + 1); ? ? ? ? } ? ? ? ? ? // 加載狀態(tài)結束 ? ? ? ? this.loading = false; ? ? ? ? ? // 數據全部加載完成 ? ? ? ? if (this.list.length >= 40) { ? ? ? ? ? this.finished = true; ? ? ? ? } ? ? ? }, 1000); ? ? }, ? }, };
4.調用api渲染頁面
導入這個接口 import { } from '@/api/xxx.js'
async onLoad () { ? const res = await 接口方法名(this.channel.id, Date.now()) ? // 獲取的數據 ? const arr = res.data.data.results // 它是一個數組 ? // 1. 追加數據到list ? // ? ?對數組進行展開 ? this.list.push(...arr) ? // 2. 把loading設置為false ? this.loading = false ? // 3. 判斷是否所有的數據全部加載完成,如果是:finished設為true ? if (arr.length === 0) { ? ? // 說明取不到數據了 ? ? this.finished = true ? } ? // 4. 頁面上提示一下用戶 ? this.$toast.success('成功加載數據') }
loading 和 finished 分別是什么含義?
List有以下三種狀態(tài),理解這些狀態(tài)有助于你正確地使用List組件:
- 非加載中,loading為false,此時會根據列表滾動位置判斷是否觸發(fā)load事件(列表內容不足一屏幕時,會直接觸發(fā))
- 加載中,loading為true,表示正在發(fā)送異步請求,此時不會觸發(fā)load事件
- 加載完成,finished為true,此時不會觸發(fā)load事件
在每次請求完畢后,需要手動將loading設置為false,表示加載結束
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
對vue2.0中.vue文件頁面跳轉之.$router.push的用法詳解
今天小編就為大家分享一篇對vue2.0中.vue文件頁面跳轉之.$router.push的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08