Vue实现底部按钮点击加载更多功能

分两种情况,第一种是一次请求所有数据并分批展示;第二种是分批次请求数据。
1、一次请求

1.1 页面

使用slice来进行限制展现从0,a的数据:

<div v-for="product in productLists.slice(0,pageSize)" :key="product.id" class="product -item">
//判断a的值是否小于数组的长度,小于就显示点击加载更多
<div class="load-more mr-bottom" v-if="a<productLists.length" @click='loadMore' >点击加载更多
</div>
<div class="load-more mr-bottom" v-else >没有更多了</div>

1.2 data
在data中定义pageSize的数值:

data() {
         return {
              pageSize: 20
         };
}

2、 分多次请求

2.1 页面

<div class="load-more mr-bottom" v-if="page<page_count"  @click='loadMore' >点击加载更多
</div>
<div class="load-more mr-bottom" v-else >没有更多了</div>

2.2 data

data() {
       return {
            page: 1,
            page_count: ''
       };
},

2.3 methods

在methods定义loadMore方法:

loadMore: function() {
            this.page += 1;
            this.getDrawPrize({
                             current_page:this.page //请求页数
          }.then(ret => {
                    console.log(ret.data.code_result)
                   this.code_result = this.code_result.concat(ret.data.code_result); //将请求回来的数据和
上一次进行组合
                })
            .catch(err => {
                    this.$toast.fail("系统开小差,请重试");
                });
       },

欢迎关注公众号Java技术大本营,会不定期分享BAT面试资料等福利。

在这里插入图片描述


  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现滚动到底部加载更多,可以借助 Vue 自带的 `scroll` 事件和一些计算属性。具体实现步骤如下: 1. 在需要滚动的容器上监听 `scroll` 事件。 ```html <template> <div ref="container" @scroll="handleScroll"> <!-- 列表内容 --> </div> </template> ``` 2. 在 `handleScroll` 方法中,判断容器滚动到了底部,如果是则触发加载更多的方法。 ```js methods: { handleScroll() { const container = this.$refs.container // 容器滚动到底部 if (container.scrollHeight - container.scrollTop === container.clientHeight) { this.loadMore() } }, loadMore() { // 加载更多数据 } } ``` 3. 计算当前是否有更多数据需要加载,用于显示加载更多按钮或者提示信息。 ```js computed: { hasMore() { return this.list.length < this.total } } ``` 4. 在模板中根据 `hasMore` 的值来显示加载更多按钮或者提示信息。 ```html <template> <div ref="container" @scroll="handleScroll"> <!-- 列表内容 --> <div v-if="hasMore" class="load-more" @click="loadMore">加载更多</div> <div v-else class="no-more">没有更多数据了</div> </div> </template> ``` 完整代码示例: ```html <template> <div ref="container" @scroll="handleScroll"> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> <div v-if="hasMore" class="load-more" @click="loadMore">加载更多</div> <div v-else class="no-more">没有更多数据了</div> </div> </template> <script> export default { data() { return { list: [], // 列表数据 total: 0, // 总共的数据量 pageSize: 10, // 每页数据量 currentPage: 1 // 当前页数 } }, computed: { hasMore() { return this.list.length < this.total } }, methods: { handleScroll() { const container = this.$refs.container // 容器滚动到底部 if (container.scrollHeight - container.scrollTop === container.clientHeight) { this.loadMore() } }, loadMore() { // 模拟加载更多数据 setTimeout(() => { const start = this.pageSize * (this.currentPage - 1) const end = start + this.pageSize this.list = this.list.concat(Array.from({ length: this.pageSize }).map((_, i) => `Item ${start + i + 1}`)) this.currentPage++ }, 500) } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值