vant中list加载异常问题

最近看list加载问题看了好久,也想了很多方法,最后自己又写了一个小demo,你们可以试一下

<template>
  <div class="home">
    <van-tabs v-model="active" @change="change">
      <van-tab title="标签 1"></van-tab>
      <van-tab title="标签 2"></van-tab>
    </van-tabs>
    <van-list
      ref="listout"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item" :title="item" />
    </van-list>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "HomeView",
  components: {
    HelloWorld,
  },
  data() {
    return {
      active: 0,
      list: [],
      loading: false,
      finished: false,
    };
  },
  methods: {
    change(value) {
      console.log(value);
      this.list = [];
      this.finished = false;

      // if (this.finished === true) {
      // this.finished = false;
      // } else {
      // this.queryData();
      // }
    },
    onLoad() {
      this.queryData();
    },
    queryData() {
      // 异步更新数据
      setTimeout(() => {
        //十个并没有铺满整个屏幕--换成20铺满整个屏幕效果一样
        console.log("发送了数据请求" + this.active);
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= 60) {
          this.finished = true;
        }
      }, 500);
    },
  },
};
</script>
<style lang="less" scoped>
.home {
  width: 100%;
  height: calc(100% - 78px);
  overflow-y: auto; //这一句很重要
}
</style>

如果还有问题,可以试试我methods注释的if判断,注意vant是可以自动检测触底的,如果想要关掉使用immediate-check设为false

第二种方式(不推荐)就比较考验数据的加载量,比如下边的20改成15就不足以撑起盒子

<template>
  <div class="home">
    <van-tabs v-model="active" @change="change">
      <van-tab title="标签 1"></van-tab>
      <van-tab title="标签 2"></van-tab>
    </van-tabs>
    <van-list
      ref="listout"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :immediate-check="false"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item" :title="item" />
    </van-list>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "HomeView",
  components: {
    HelloWorld,
  },
  data() {
    return {
      active: 0,
      list: [],
      loading: false,
      finished: false,
    };
  },
  created() {
    this.onLoad();
  },
  methods: {
    change(value) {
      console.log(value);
      this.list = [];
      this.finished = false;
      this.queryData();
      // if (this.finished === true) {
      //   this.finished = false;
      // } else {
      //   this.queryData();
      // }
    },
    onLoad() {
      this.queryData();
    },
    queryData() {
      // 异步更新数据
      setTimeout(() => {
        console.log("发送了数据请求" + this.active);
        for (let i = 0; i < 20; i++) {
          this.list.push(this.list.length + 1);
        }
        // 加载状态结束
        this.loading = false;
        if (this.list.length >= 40) {
          this.finished = true;
        }
      }, 500);
    },
  },
};
</script>
<style lang="less" scoped>
.home {
  width: 100%;
  // height: calc(100vh - 78px);
  // overflow-y: auto;
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值