vant-list上滑加载,数据重复,加载后返回顶部,或者不加载更多数据

(1)vant-list上滑加载

list这个组件,必须具有高度,才能实现上滑加载,不然不会有上滑加载的过程。 

因为没有高度,相当于整个屏幕都是组件的,就没有底部一说,所以会一直加载所有的,

类似下面的列表,是有高度的。

方法:

  • 可以用flex布局

.view {

display: flex;

flex-direction: column;

flex-wrap: nowrap;

width: 100%;

height: 100%;

}

.header {

}

.content {

overflow-y: scroll;

}

list用vant的list就可以。

<List

            class="drop-list"

            v-model="loading"

            :finished="finished"

            finished-text="没有更多了"

            :offset="10"

            @load="onLoad"

        >

            <ReferraldoctorList v-for="(item,index) in doctorList" :key="index" :item="item"></ReferraldoctorList>

        </List>

(2)搜索后数据重复

onsearch中直接执行onLoad,不要直接调用接口,不然接口和onLoad都会执行一遍

onSearch(value){

            this.init()

            // this.getArtivleList(this.isAll, this.typeValue)

            this.onLoad()

        },

(3)加载后,list回到顶部

因为请求后台接口时,统一加了toast  Loading的提示,且设置了 forbidClick: true,

解决办法,请求后台接口时,加一个setTimeout

onLoad() {

            this.loading = true

            if(!this.finished){

                setTimeout(() => {

                    this.getZJDoctortList()

                }, 200);

            }

        },

(4)上滑没有加载动作

如果没有加载动作,需要检查finished的值,如果finished为true,就不会再触发上啦加载的动作了,finished只有在返回的数据量》=total时,才会为true

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现滚动到底部加载下一页数据,可以使用 `van-list` 中的 `@scroll.native` 事件来监听滚动事件,然后通过计算列表高度和滚动高度来判断是否滚动到了底部。 具体实现步骤如下: 1. 给 `van-list` 组件添加 `ref` 属性,用于获取组件实例。 ```html <van-list ref="list" :finished="isFinished" :loading="isLoading" @load="onLoad"> ... </van-list> ``` 2. 在 `mounted` 钩子函数中,获取 `van-list` 组件实例,并给 `window` 对象添加 `scroll` 事件监听器。 ```typescript import { defineComponent, onMounted, ref } from 'vue'; import { List } from 'vant'; export default defineComponent({ components: { VanList }, setup() { const listRef = ref(null); onMounted(() => { const listInstance = listRef.value.$el; window.addEventListener('scroll', () => { const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; const clientHeight = document.documentElement.clientHeight || document.body.clientHeight; if (scrollTop + clientHeight >= scrollHeight && listInstance.offsetHeight) { // 滚动到底部,触发加载更多 } }); }); return { listRef, }; }, }); ``` 3. 在 `@load` 事件中,将 `isFinished` 和 `isLoading` 属性设置为 `true` 和 `false`,表示数据已经加载完成或正在加载中。 ```typescript export default defineComponent({ components: { VanList }, setup() { const listRef = ref(null); const isFinished = ref(false); const isLoading = ref(false); const onLoad = () => { if (!isFinished.value && !isLoading.value) { isLoading.value = true; // 发送网络请求,获取下一页数据 // 请求成功后,将 isLoading 设置为 false,表示加载完成 } }; onMounted(() => { const listInstance = listRef.value.$el; window.addEventListener('scroll', () => { const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; const clientHeight = document.documentElement.clientHeight || document.body.clientHeight; if (scrollTop + clientHeight >= scrollHeight && listInstance.offsetHeight) { onLoad(); } }); }); return { listRef, isFinished, isLoading, onLoad, }; }, }); ``` 这样,当滚动到底部时,就会触发 `onLoad` 方法,从而加载下一页数据。需要注意的是,在数据全部加载完成后,需要将 `isFinished` 设置为 `true`,以防止重复加载数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值