vant之van-list滑到底部不触发问题

  问题:在首次点击查询列表数据触底会触发pageNo++事件,在第二次点击查询列表时触底事件不执行。

原因:是在每一次点击查询列表时都需要将原有的loading参数与finished参数恢复默认值。如若未将finished恢复值,将会出现下拉时提示暂无更多数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值