vue实现点击加载更多

1.定义两个布尔型的变量,一个是默认显示的数,一个是默认加载的数loading和finished

2.v-if条件渲染,如果值为true,就会编译,如果为false,那么页面将不会生成html元素/标签

<template>
  <!-- 展示商品页面 -->
  <div class="name">
    <van-grid :column-num="2">
      <div v-for="item in records.slice(0, length)" :key="records.index"> <van-col class="name"><van-image width="10rem"
            height="10rem" fit="contain" :key="item.index" :src="item.image" /><br>名称:{{ item.bookname }}<br>价格:{{
              item.price
            }}</van-col></div>

    </van-grid>
   
    <p v-if="loading" @click="more()" class="btn">
      点击加载更多
      <!-- <van-icon name="arrow-down" /> -->
    </p>
    <p v-if="finished" @click="more()" class="btn">没有更多了</p>
  </div>
</template>
<script>
import { fetchList } from '@/api/book'
export default {
  data() {
    return {
      num: 6, //默认加载数
      length: 8,  //数组显示的元素个数
      // isFlag: true,
      listQuery: {
        page: 1,
        limit: 40,
        bookname: ""
      },
      // error:false,
      loading: true,
      finished: false,
      records: [
        {
          id: 0,
          bookname: '',
          price: 0.0,
          image: ''
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      fetchList(this.listQuery).then(response => {
        console.log(response.data)
        this.records = response.data.data.records
      })
    },
    more() {
      for (let i = 1; i < 4; i++) {
        if (this.length < this.records.length) {

          this.length += this.num;
          this.loading = true
        }
        if (this.length >= this.records.length) {
          this.loading = false
          this.finished = true
        }
        break
      }
    },
  }
}
</script>
<style>
.btn {
  text-align: center;

}
</style>

点击加载更多,新显示6个图

数据库的数据全都加载完后,在底部显示没有更多了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值