Vue Element UI v-infinite-scroll无限触发问题解决+样例代码

官方文档:

ElementUI —— InfiniteScroll 无限滚动

https://element.eleme.cn/#/zh-CN/component/infiniteScroll

根据文档这部分,写了我的代码,开始长下面这样子…结果发现了一个问题,就是进入页面之后像是  infinite-scroll-disabled和infinite-scroll-immediate没有生效似的,页面疯狂调用onLoad函数。

<div
  class="left"
  v-infinite-scroll="onLoad()"
  infinite-scroll-disabled="disabled"
  infinite-scroll-immediate="false"
>
  <div v-for="item in demoList" :key="item.commentId"
    <list-item :item="item" ></list-item>
  </div>
  <p v-if="listParams.loading">Loading...</p>
  <p v-if="listParams.finished">No More</p>
</div>

度娘许久无果,于是逐字对比官方样例……最终将

v-infinite-scroll="onLoad()"

改为

v-infinite-scroll="onLoad"

成功解决问题…

 

放一个能正常运行的代码上来

<template>
  <div class="container">
    <div
      class="left"
      v-infinite-scroll="onLoad"
      infinite-scroll-disabled="disabled"
      infinite-scroll-immediate="false"
    >
      <div v-for="item in demoList" :key="item.commentId">
        <list-item
          :item="item"
        ></list-item>
      </div>
      <p v-if="listParams.loading">Loading...</p>
      <p v-if="listParams.finished">No More</p>
    </div>
  </div>
</template>

<script>
import ListItem from "../../components/ListItem";
export default {
  data() {
    return {
      demoList: [],
      listParams: {
        pageNum: 1,
        pageSize: 10,
        loading: false,
        error: false,
        finished: false,
      },
    };
  },
  components: {
    ListItem,
  },
  created() {
    this.getDemoList();
  },
  mounted() {},
  computed: {
    disabled() {
      console.log(
        this.listParams.loading || this.listParams.finished
      );
      return this.listParams.loading || this.listParams.finished;
    },
  },
  methods: {
    onLoad() {
      this.listParams.loading = true;
      if (this.listParams.finished == false) {
        this.listParams.pageNum++;
        this.getDemoList();
      }
    },
    getDemoList() {
      var that = this;
      var params = {
        pageNum: that.listParams.pageNum,
        pageSize: that.listParams.pageSize,
      };
      this.$api.myHistoryController
        .getDemoList(params)
        .then((res) => {
          if (res.status == 1000) {
            if (res.body.list.length > 0) {
              that.demoList = that.demoList.concat(res.body.list);
              that.listParams.loading = false;
              if (res.body.list.length < that.listParams.pageSize) {
                that.listParams.finished = true;
              }
            } else {
              that.listParams.loading = false;
              that.listParams.finished = true;
            }
          }
          that.listParams.loading = false;
        })
        .catch(function () {
          that.listParams.error = true;
          that.listParams.loading = false;
        });
    },
  },
};
</script>

<style scoped>
#header {
  display: none;
}
.container {
  display: flex;
  flex-direction: row;
  height: 100%;
  padding: 10px;
}
.left {
  width: 400px;
  flex-shrink: 0;
  background-color: white;
  overflow: auto;
}
</style>

 

可以使用 Element UI 中的 v-infinite-scroll 指令来实现鼠标滚轮滚动带多列文字有标题的文字栏。v-infinite-scroll 可以在滚动到指定元素底部时触发指定的回调函数,因此可以用于实现滚动加载。 以下是示代码: ```html <template> <div class="text-column" v-infinite-scroll="loadMore" infinite-scroll-disabled="loading"> <div class="title">{{ title }}</div> <div class="text-container"> <div class="column" v-for="text in textList" :key="text">{{ text }}</div> </div> <div v-if="loading" class="loading">Loading...</div> </div> </template> <script> export default { data() { return { title: "My Text Column", textList: [ "Column 1 Text 1", "Column 1 Text 2", "Column 1 Text 3", "Column 1 Text 4", "Column 1 Text 5", "Column 2 Text 1", "Column 2 Text 2", "Column 2 Text 3", "Column 2 Text 4", "Column 2 Text 5", "Column 3 Text 1", "Column 3 Text 2", "Column 3 Text 3", "Column 3 Text 4", "Column 3 Text 5" ], loading: false }; }, methods: { loadMore() { // 模拟异步加载数据 this.loading = true; setTimeout(() => { this.textList = this.textList.concat([ "Column 1 Text 6", "Column 2 Text 6", "Column 3 Text 6" ]); this.loading = false; }, 1000); } } }; </script> <style> .text-column { display: flex; flex-direction: column; align-items: center; overflow-y: scroll; height: 300px; } .title { font-size: 24px; font-weight: bold; margin-bottom: 10px; } .text-container { display: flex; flex-wrap: wrap; justify-content: space-between; width: 100%; } .column { width: calc(33.33% - 10px); margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; box-sizing: border-box; } .loading { margin-top: 20px; font-size: 16px; color: #999; } </style> ``` 在上面的代码中,我们使用了 Element UI 的 v-infinite-scroll 指令,并将其应用于包含多列文本和标题的 div 元素上。我们还设置了 infinite-scroll-disabled 属性,以防止在加载更多数据时重复触发回调函数。 当用户滚动到底部时,loadMore 方法将被调用,并模拟异步加载更多数据。在加载数据时,我们将 loading 设置为 true,以显示加载动画。当数据加载完成时,我们将新数据添加到 textList 中,并将 loading 设置为 false,以隐藏加载动画。 通过这种方式,我们就可以使用 Element UI 的 v-infinite-scroll 指令来实现一个带有多列文本和标题的滚动文字栏,并支持鼠标滚轮滚动和滚动加载。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值