uniapp上拉加载

喜欢ヅ旅行 2021-07-25 23:04 697阅读 0赞

最近在写uniapp的项目,看好写到上拉加载这一块,拿出来详细说一下写法,和注意事项吧。
首先在data里面要有这些值

  1. last_page: '', //总页数
  2. page: 2,//上拉加载的起始页
  3. loadingnum: 12, //加载条数
  4. send:false,//上拉加载的状态 避免数据没更新时重复请求
  5. productlist:[],//数据盒子
  6. nodata: false, //没有更多数据
  7. loadings: false, //加载框

然后和onShow或者onLoad同级的地方写onReachBottom()

  1. //上拉加载
  2. onReachBottom() {
  3. //判断总页数是否大于1
  4. if (this.last_page > 1) {
  5. if(this.send == false){
  6. if (this.page <= this.last_page) {
  7. //开始加载
  8. this.send = true;
  9. this.loadings = true;//这个是上拉的特效
  10. this.$api.get(global.apiUrls.productlist, {
  11. // status:this.current,
  12. page: this.page,
  13. pagesize: this.loadingnum,
  14. type: 2,
  15. })
  16. .then(res => {
  17. const {
  18. productlist
  19. } = this;
  20. if(res.data.code == 1){
  21. //延迟加载数据 减少并发量
  22. setTimeout(() => {
  23. //结束加载
  24. this.loadings = false;
  25. this.productlist = [...productlist, ...res.data.data.data]
  26. this.send = false;
  27. }, 700)
  28. this.page++;
  29. }else{
  30. this.$message.info('诶呀,加载失败了稍后再试试吧');
  31. this.send = false;
  32. }
  33. }).catch(err => {
  34. // console.log(res)
  35. this.send = false;
  36. })
  37. } else {
  38. this.nodata = true; //当加载完没数据后 显示无更多数据
  39. }
  40. }
  41. }
  42. },

在methods中写第一次获取数据的方法

  1. //获取数据
  2. getproductlist() {
  3. this.$api.post(global.apiUrls.productlist, {
  4. page: 1,
  5. pagesize: this.loadingnum,
  6. type: 2, //获取数据类型
  7. })
  8. .then(res => {
  9. console.log(res.data, 1111111111)
  10. if(res.data.code == 1){
  11. this.productlist = res.data.data.data;
  12. this.last_page = res.data.data.last_page;
  13. if (res.data.data.last_page == 1) {
  14. this.nodata = true;
  15. }
  16. } else{
  17. this.$message.info('唉,获取数据失败了');
  18. this.nodata = true;
  19. }
  20. }).catch(err => {
  21. console.log(res)
  22. })
  23. },

onLoad里面调用 getproductlist , 不要放在onShow里面,因为一般都上拉加载处都是列表,点击是可以进入详情页的,如果放到onShow里面会造成每次从详情页返回都会重新获取数据,造成页面刷新,以及内容回到最上面。

  1. this.getproductlist();//获取数据

如果有一个界面有其他分类的,在切换类型时,切记要重置 page(上拉起始页),也可以将总页数和上拉起始页一起重置,可以在切换方法里加入

  1. this.page = 2;//将上拉起始页重置到 2 **这部是必须要有的**
  2. this.last_page = '',//将总页数置空

如果页面有下拉刷新,同样切记一定要将 page重置到2 代码同上;

加载特效 使用了uview组件
底部loading 和无更多数据
html

  1. <view class="loading" v-show="loadings">
  2. <view class="loading-san">
  3. <u-loading size="40" color="#FFBA4B" ></u-loading>
  4. <view class="logintext">一大波玩偶正在赶来~~</view>
  5. </view>
  6. </view>
  7. <view class="nodata" v-show="nodata">
  8. 没有更多数据
  9. </view>

css

  1. //加载
  2. .loading {
  3. width: 100%;
  4. height: 100upx;
  5. display: flex;
  6. justify-content: space-around;
  7. align-items: center;
  8. background-color: #F6F7F9;
  9. .loading-san {
  10. display: flex;
  11. justify-content: space-around;
  12. align-items: center;
  13. .logintext {
  14. margin-left: 16upx;
  15. font-size: 28upx;
  16. font-family: PingFang;
  17. color: #999999;
  18. }
  19. }
  20. }
  21. //没有更多数据
  22. .nodata {
  23. width: 100%;
  24. height: 100upx;
  25. background-color: #F6F7F9;
  26. text-align: center;
  27. line-height: 100upx;
  28. color: #999999;
  29. font-size: 24upx;
  30. font-family: PingFang SC;
  31. font-weight: 400;
  32. }

发表评论

表情:
评论列表 (有 0 条评论,697人围观)

还没有评论,来说两句吧...

相关阅读

    相关 uniapp

    最近在写uniapp的项目,看好写到上拉加载这一块,拿出来详细说一下写法,和注意事项吧。 首先在data里面要有这些值 last_page: '', //总页数