微信小程序上拉加载

微信小程序中上拉加载有两种方式:1、通过scroll-view标签 2、内置的生命周期函数onReachBottom

(一)scroll-view标签

wxml

​
<scroll-view scroll-y style="width:100%;height:calc(100vh - 116rpx);" bindscrolltolower="loadMore"><-- 一定要注意高度的设置 -->
    <block wx:for="{{list}}" wx:key="{{item.id}}">
      <view class="lawItem">
        <text class="lawTitle">{{item.title}}</text>
        <text class="lawDate">{{item.date}}</text>
      </view>
    </block>
  </scroll-view>

​

js

/**
   * 加载更多
   */
  loadMore() {
    let _this = this;
    if (!_this.data.isEmpty) {
      _this.setData({
        currentPage: _this.data.currentPage++
      })
      _this.getData();
    }
  },
  /**
   * 获取信息列表
   */
  getData() {
    let _this = this;
    let params = {}
    wxRequest('', params, 'POST', 'loading').then(res => {
      if (res.data.status == 1) {
        if (JSON.stringify(res.data.data) != '[]') {
          _this.setData({
            list: [...list, ...res.data.data]
          })
        } else {
          _this.setData({
            isEmpty: true  //isEmpty默认为false
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

(二)利用onReachBottom生命周期回调(这种方法适用的情况比较少,更多的是使用scroll-view)

参考监听上拉触底事件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值