官方文档:

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>

 

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45 7 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐