您好,登錄后才能下訂單哦!
van-list方法如何正確的在vant 中使用?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
van-list里面的元素不能有float樣式,否則會(huì)連續(xù)觸發(fā) load 事件
原代碼
<template> <div class="about"> <van-tabs v-model="active" sticky @change="getTypeDate"> <van-tab v-for="(tab) in typeList" :title="tab.name" :key="tab.id"> <div : class="pic-content"> <van-list :finished="finished" :finished-text="finishedText" v-model="loading" :offset="10" :immediate-check="false" @load="getserviceList" > <!------------------------------------------------- 修改前代碼 ---------------------------------------------> /*<div class="pic-box" v-for="(serve) in serviceList" :key="serve.id" @click="router(serve)" > <div class="pic-item"> <img v-if="serve.picturePath" :src="$BASE_PICTUREPATH_URL + serve.picturePath.split(',')[0]" > </div> <p>{{serve.name}}</p> <p class="price-red">¥{{serve.price}}</p> </div>*/ <!------------------------------------------------- 修改前代碼 ---------------------------------------------> </van-list> </div> </van-tab> </van-tabs> </div> </template>
<script> import { Tab, Tabs, List, Cell, Row, Col } from "vant"; import { FetchServeType, FetchServeList } from "../apis/serve.js"; export default { data() { return { active: 0, typeList: [], serviceList: [], type: "", finishedText: "", finished: false, pageNum: 1, pageSize: 10, contentHeight: 0, loading: false }; }, mounted() { this.getOrderStyle(); this.contentHeight = document.documentElement.clientHeight - 66 - 40 + "px"; }, methods: { async getOrderStyle() { let res = await FetchServeType(); if (res.data && res.data.success) { this.typeList = res.data.data; this.type = res.data.data[0].name; this.getTypeDate(); } }, getTypeDate() { this.pageNum = 1; this.type = this.typeList[this.active].name; this.serviceList = []; this.finishedText = ""; this.finished = false; this.getserviceList(); }, async getserviceList() { let toast = this.$toast.loading({ mask: true, message: "加載中..." }); const { type, pageNum, pageSize } = this; let params = { type, pageNum, pageSize }; let res = await FetchServeList(params); this.loading = false; toast.close(); if (res.data && res.data.success) { let list = (res.data.data && res.data.data.list) || []; if (pageNum > 1) { this.serviceList = [...this.serviceList, ...list]; } else { this.serviceList = list; } // 如果當(dāng)前頁(yè)數(shù) = 總頁(yè)數(shù),則已經(jīng)沒有數(shù)據(jù) if (res.data.data.pageNum === res.data.data.pages) { this.finished = true; this.finishedText = "- 沒有更多了-"; } // 如果總頁(yè)數(shù)大于當(dāng)前頁(yè)碼,頁(yè)碼+1 if (res.data.data.pages > pageNum) { this.pageNum++; } } console.log("FetchServeList: ", this.serviceList); } } }; </script>
<style lang="scss" scoped> .pic-content { overflow-y: scroll; -webkit-overflow-scrolling: touch; .pic-box { /****************************修改前代碼***************************/ background-color: #fff; overflow: hidden; break-inside: avoid; box-sizing: border-box; margin-bottom: 0.7rem; padding: 0.8rem; width: 48%; height: 16rem; ~~float: left;~~ /**************不能有float樣式*************/ margin: 1%; border-radius: 4px; /****************************修改前代碼***************************/ p:nth-of-type(1) { padding: 0.8rem 0; } p:nth-of-type(2) { color: red; } .pic-item { height: 11rem; flex-direction: column; justify-content: center; overflow: hidden; img { width: 100%; height: auto; border-radius: 4px; } } } } </style>
// 修改后代碼(注釋部分為修改后代碼)
<template> <div class="about"> <van-tabs v-model="active" sticky @change="getTypeDate"> <van-tab v-for="(tab) in typeList" :title="tab.name" :key="tab.id"> <div : class="pic-content"> <van-list :finished="finished" :finished-text="finishedText" v-model="loading" :offset="10" :immediate-check="false" @load="getserviceList" > <!------------------- 修改后代碼 --------------------> /*<van-row> <van-col span="12" class="pic-box" v-for="(serve) in serviceList" :key="serve.id" @click="router(serve)" > <div class="pic-item"> <img v-if="serve.picturePath" :src="$BASE_PICTUREPATH_URL + serve.picturePath.split(',')[0]" > </div> <p>{{serve.name}}</p> <p class="price-red">¥{{serve.price}}</p> </van-col> </van-row>*/ <!------------------- 修改后代碼 --------------------> </van-list> </div> </van-tab> </van-tabs> </div> </template>
<script> import { Tab, Tabs, List, Cell, Row, Col } from "vant"; import { FetchServeType, FetchServeList } from "../apis/serve.js"; export default { data() { return { active: 0, typeList: [], serviceList: [], type: "", finishedText: "", finished: false, pageNum: 1, pageSize: 10, contentHeight: 0, loading: false }; }, mounted() { this.getOrderStyle(); this.contentHeight = document.documentElement.clientHeight - 66 - 40 + "px"; }, methods: { async getOrderStyle() { let res = await FetchServeType(); if (res.data && res.data.success) { this.typeList = res.data.data; this.type = res.data.data[0].name; this.getTypeDate(); } }, getTypeDate() { this.pageNum = 1; this.type = this.typeList[this.active].name; this.serviceList = []; this.finishedText = ""; this.finished = false; this.getserviceList(); }, async getserviceList() { let toast = this.$toast.loading({ mask: true, message: "加載中..." }); const { type, pageNum, pageSize } = this; let params = { type, pageNum, pageSize }; let res = await FetchServeList(params); this.loading = false; toast.close(); if (res.data && res.data.success) { let list = (res.data.data && res.data.data.list) || []; if (pageNum > 1) { this.serviceList = [...this.serviceList, ...list]; } else { this.serviceList = list; } // 如果當(dāng)前頁(yè)數(shù) = 總頁(yè)數(shù),則已經(jīng)沒有數(shù)據(jù) if (res.data.data.pageNum === res.data.data.pages) { this.finished = true; this.finishedText = "- 沒有更多了-"; } // 如果總頁(yè)數(shù)大于當(dāng)前頁(yè)碼,頁(yè)碼+1 if (res.data.data.pages > pageNum) { this.pageNum++; } } console.log("FetchServeList: ", this.serviceList); } } }; </script>
<style lang="scss" scoped> .pic-content { overflow-y: scroll; -webkit-overflow-scrolling: touch; .pic-box { /************************ 修改后代碼**************************/ background-color: #fff; overflow: hidden; box-sizing: border-box; margin-bottom: 0.7rem; padding: 0.8rem; height: 16rem; border-radius: 4px; /************************ 修改后代碼************************ **/ p:nth-of-type(1) { padding: 0.8rem 0; } p:nth-of-type(2) { color: red; } .pic-item { height: 11rem; flex-direction: column; justify-content: center; overflow: hidden; img { width: 100%; height: auto; border-radius: 4px; } } } } </style>
補(bǔ)充知識(shí):vant里 List 組件可以與 PullRefresh 組件結(jié)合使用的一個(gè)小提示與小坑坑
小提示
List 組件可以與 PullRefresh 組件結(jié)合使用,可以實(shí)現(xiàn)列表下拉刷新的效果,但是當(dāng)下拉刷新后更新的數(shù)據(jù)展示在頁(yè)面上不能撐滿 List 列表中的內(nèi)容的時(shí)候,他并不會(huì)主動(dòng)觸發(fā)列表刷新,以至于來(lái)填滿列表。
可以給list組件添加ref屬性,然后在下拉刷新后,在下拉刷新的事件里手動(dòng)調(diào)用this.$refs.listRef(你的list的ref名稱).check()來(lái)觸發(fā)列表加載后續(xù)的數(shù)據(jù)
// list組件 <van-list v-model="loading" ref="listRef" // 1. 綁定ref :finished="finished" finished-text="沒有更多了" :error.sync="error" error-text="請(qǐng)求失敗,點(diǎn)擊重新加載" @load="onLoad" > // 下拉刷新的事件 onRefresh() { ...刷新成功后 // 2.手動(dòng)去讓下拉刷新后,去執(zhí)行l(wèi)ist列表的load事件 this.$refs.listRef.check() }
小坑坑
如果你把List 組件可以與 PullRefresh 組件結(jié)合使用封裝成一個(gè)組件,然后在父組件中使用的時(shí)候,需要給封裝的這個(gè)組件傳list組件的v-model的值來(lái)控制list是否處于加載狀態(tài)。
然后在父組件傳 v-moel=“l(fā)oading” 或者 :is-loading.sync=“l(fā)oading” 傳給子組件讓他來(lái)控制子組件的list的v-model的控制load加載狀態(tài),按理說(shuō)v-model 默認(rèn)是 value 屬性和 input 事件的組合,但是list組件的文件默認(rèn)修改了,把傳過(guò)去的value用 model: { prop: ‘loading' }修改了,所以我們?cè)谧咏M件接收的時(shí)候不能用value 要用loading
此圖為vant的源碼
// 父組件 給子組件傳list的v-model的值 :is-loading.sync="loading" // 或?qū)懗? v-model="loading" // 子組件 list組件 // 子組件不能用value接收 // :value="isLoading" // 應(yīng)該寫成loading :loading="isLoading"
關(guān)于van-list方法如何正確的在vant 中使用問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。