当前位置: 代码网 > it编程>编程语言>Javascript > uniapp中uni-load-more的使用方式

uniapp中uni-load-more的使用方式

2024年06月11日 Javascript 我要评论
uniapp中uni-load-more使用1 引入uniloadmoreimport uniloadmore from '@/components/uni-load-more/uni-load-mo

uniapp中uni-load-more使用

1 引入uniloadmore

import uniloadmore from '@/components/uni-load-more/uni-load-more.vue';
components: {uniloadmore},

2 data中写的内容

reload: false,
status: 'more',
contenttext: {
        contentdown: '上拉加载更多~',
        contentrefresh: '加载中',
        contentnomore: '我是有底线的~'
},

3 template里面写的内容

<uni-load-more :status="status" :icon-size="14" :content-text="contenttext" v-if="datalist.length > 0" />

4 请求接口成功之后,判断加载状态,处理数据

success: (result) => {
        this.totalcount = result.data.total
        if (result.data.total > 0) {
                const datamap = result.data.list
                this.datalist = this.reload ? datamap : this.datalist.concat(datamap);
                this.reload = false;
        } else {
                this.datalist = [];
        }
        if (this.totalcount == this.datalist.length) {
                this.reload = false;
                this.status = 'nomore'
        }
}

5 监控加载状态

onreachbottom() {
        if (this.totalcount > this.datalist.length) {
                this.status = 'loading';
                settimeout(() => {
                        this.pagenum++
                        this.getmonthtask();//执行的方法
                }, 1000)//这里我是延迟一秒在加载方法有个loading效果,如果接口请求慢的话可以去掉
        } else { //停止加载
                this.status = 'nomore'
        }
},

uniapp - load-more触底加载,下拉刷新

底部加载load-more(uni-ui组件)

三个状态:more、loading、nomore

  • 触底事件:onreachbottom
  • 下拉刷新:onpulldownrefresh,停止下拉刷新uni.stoppulldownrefresh()
<template>
  <view>
    <!-- 底部加载,三个状态:more、loading、nomore -->
    <uni-load-more :status="status"></uni-load-more>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      data: null,
      status: 'more', //触底加载状态
      page: 1, //记录当前页码
    };
  },
  //触底事件,请求数据、合并
  onreachbottom() {
    console.log('到底了到底了...');
    this.status = 'loading';
    this.getdata(this.page + 1);
    this.page += 1;
  },
  //下拉刷新,请求第一页
  onpulldownrefresh() {
    this.getdata();
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata(page = 1) {
      uni.request({
        url: 'https://xxxx.....',
        method: 'get',
        data: { page: page },
        success: res => {
          console.log(res);
          //如果页数>1,需要拼接返回的数据
          if (page > 1) {
            res.data.result = [...this.data.result, ...res.data.result];
          }
          this.data = res.data;
          uni.stoppulldownrefresh(); //拿到数据后,停止下拉刷新
        },
        fail: () => {},
        complete: () => {}
      });
    },
  },
};
</script>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com