小程序加载云端数据库中的第二页数据,前端如何动态显示?

export default class HelloLoading{

  constructor(collection_name,db){
    this.collection_name = collection_name

    this.db = db
    this.setPageCount(db)
  }

  pageCount = 0;
setPageCount(db){ db.collection(this.collection_name).count({ success:function(res){ // console.log(res.total) this.pageCount = res.total }, fail: console.error }) }
getPageCount(){ return this.pageCount } }

  

如上,当请求getPageCount() 的时候,构造函数的setPageCount还没有执行结束呢!怎么办?

回调函数!

export default class HelloLoading{

  static pageCount = 0;

  constructor(collection_name){
    this.collection_name = collection_name

    this.db = wx.cloud.database()
   
  }

  setPageCount(callback){

    this.db.collection(this.collection_name).count({
      success:function(res){
        
        console.log(res.total)
        HelloLoading.pageCount = res.total

        callback()
      },
      fail: console.error
    })

  }

  getPageCount(){
    return HelloLoading.pageCount;
  }

}

  外部调用的时候,使用

loadingdata.setPageCount(function(){
      console.log(loadingdata.getPageCount())
    })

  

原文地址:https://www.cnblogs.com/xixiaohui/p/12168141.html