Mint-ui-infinite-scroll解决重复加载

第一种方法(当数据不多的情况下使用【比如排行榜10条】)

思路

每次上滑的时候都调一次接口,直到没有数据就将infinite-scroll-disabled 设为true,每次截取slice(i * 3, (i + 1) * 3); 3是每次显示的条数

代码

<div class="r-contatin">
  <ul
    class="r-item-wrapper"
    v-infinite-scroll="loadMore"
    infinite-scroll-disabled="loading"
    infinite-scroll-distance="10"
  >
    <li v-for="(item, key) in list" :key="key">
      <div>{{item}}=={{key}}</div>
    </li>
  </ul>
</div>

<p v-if="!noMore" class="loading-tip">
  <mt-spinner color="rgba(255, 51, 153, 1)" type="fading-circle"></mt-spinner>
  <span style="margin-left: 5px;">更多加载中...</span>
</p>
<p class="no-more" v-else>没有更多了~</p>

<p v-if="!noMore" class="loading-tip">
  <mt-spinner color="rgba(255, 51, 153, 1)" type="fading-circle"></mt-spinner>
  <span style="margin-left: 5px;">更多加载中...</span>
</p>
<p class="no-more" v-else>没有更多了~</p>
data() {
  return {
    list: [],
    moreList: [],
    i: 0,
    loading: false,
    noMore: false,
  };
},
filters: {
},
mounted() {
  this.getData(this.obj);
},
methods: {
  getData(obj) {
    let i = 0;
    let that = this;
    axios.post(obj)
      .then(res => {
        console.log(res);
        if (res.data.code === "200") {
          that.list = res.data.result.rankingDetailList.slice(
            i * 3,
            (i + 1) * 3
          );
        } else {
          Toast("载入失败");
        }
      })
      .catch(err => {
        Toast("载入失败");
        console.log(err);
      });
  },
  loadMore() {
    let that = this;
    that.loading = true;
    that.noMore = false;
    axios.post(that.obj)
      .then(res => {
        that.i++;
        console.log(res);
        that.moreList = res.data.result.rankingDetailList.slice(
          that.i * 3,
          (that.i + 1) * 3
        );
        if (that.moreList.length === 0) {
          that.noMore = true;
          that.loading = true;
        } else {
          that.loading = false;
          that.noMore = false;
          that.moreList.forEach(function(item) {
            that.list.push(item);
          });
        }
      })
      .catch(function(error) {
        console.info(error);
      });
  },
}

第二种方法(当数据很多并有分页的情况下使用【比如点菜菜单 >= 20 条】)

思路

第一次进来传第一页,之后每次上滑page++,当返回的数据长度为0时,把上滑功能关闭
##代码

<div class="real-content" v-infinite-scroll="busy" infinite-scroll-distance="10">
  <div class="item" v-for="g in good" :key="g.item">
    ...
  </div>
</div>
loadMore(){
	this.busy = true;
	this.pageSize++;
  axios({
    //请求数据
	})
  this.busy = false;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值