微信小程序 如何在组件中实现 上拉加载下一页和下拉触底

通过在父页面中使用selectComponent来调用子组件的方法来实现

1、在component中配置好方法

子页面homePage/index/index.js
javascript 复制代码
// homePage/index/index.js
var total = 0
var pageNo = 1
const pageSize = 20
Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  lifetimes: {
    created: function () {
    },
    attached: function () {
      setTimeout(() => {
        this.getProfessorQuestionList('fresh')
      }, 0)
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    questionList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadData(action) {
      if (action == 'fresh') {
        pageNo = 1
      }
      if (action == 'more') {
        if (pageNo * pageSize < total) {
          pageNo++
          this.getProfessorQuestionList('more')
        }
      } else {
        this.getProfessorQuestionList('fresh')
      }
    },
    getProfessorQuestionList(type) {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      httpUtils.post({
        url: url,
        data: {
          page: pageNo,
          pageSize: pageSize,
        }
      }).then(res => {
        wx.hideLoading()
        total = res.page.total;
        let questionList = stringUtils.isNull(res.list) ? [] : res.list;
        if (type == 'fresh') {
          this.setData({
            questionList: questionList
          })
        } else {
          this.setData({
            questionList: this.data.questionList.concat(questionList)
          })
        }
      }).finally(() => {
        wx.hideLoading()
         this.setData({
          noData:this.data.questionList.length? true:false
        })
      })
    },
  }
})

2、在父页面使用selectComponent来调用子组件的方法

父页面page/index/index.json
javascript 复制代码
{
  "usingComponents": {
    "homePage": "../../homePage/index/index",
  },
  "navigationStyle": "custom"
}
父页面page/index/index.wxml
javascript 复制代码
 <homePage id="homePage" />
父页面page/index/index.js
javascript 复制代码
Page({
  data: {
    homePage: ''
  },

  onReady() {
    this.setData({
      homePage: this.selectComponent("#homePage"),
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.data.homePage.loadData('fresh')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.homePage.loadData('more')
  },
})
相关推荐
贝格前端工场27 分钟前
小程序订阅消息设计:用户触达与隐私保护的平衡法则
大数据·小程序
weixin_lynhgworld30 分钟前
盲盒一番赏小程序:用科技重新定义“未知的快乐”
科技·小程序
Bruce_Json1 小时前
微信小程序ts+sassjlin-ui
微信小程序·小程序·sass
来碗盐焗星球3 小时前
记一次微信小程序AI开发的血泪史
前端·微信小程序
说私域4 小时前
淘宝直播与开源链动2+1模式AI智能名片S2B2C商城小程序的融合发展研究
人工智能·小程序·开源
说私域7 小时前
互联网生态下赢家群体的崛起与“开源AI智能名片链动2+1模式S2B2C商城小程序“的赋能效应
人工智能·小程序·开源
The_era_achievs_hero14 小时前
微信小程序71~80
微信小程序·小程序
dssxyz16 小时前
uniapp打包微信小程序主包过大问题_uniapp 微信小程序时主包太大和vendor.js过大
javascript·微信小程序·uni-app
!win !16 小时前
被老板怼后,我为uni-app项目引入环境标志
前端·小程序·uni-app
澄江静如练_20 小时前
微信小程序发体验版
微信小程序·小程序