鍍金池/ 問答/HTML/ 微信小程序this.setData異步的問題。

微信小程序this.setData異步的問題。


Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    category:[],
    ctime:[],
    target:[],
    addres:[],
    index: 0,
    indextarget:0,
    indexcategory:0,
    indexctime:0,
    indexaddres:0,
    addresinit:true,
    ctimeinit:true,
    targeinit:true,
    categoryinit:true,
    menuTapCurrent:0,
    selectaddres:"",
    selectctime:""
  },
  
  bindaddresChange:function(e) {
    // console.log('picker發(fā)送選擇改變,攜帶值為', e.detail.value)
    this.setData({
      indexaddres: e.detail.value,
      addresinit:false,
      selectaddres:this.data.addres[this.data.indexaddres]  //第一步
    })
    console.log(this.data.addres[this.data.indexaddres],"選擇地址")
    this.schoolList() 
  },
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  schoolList:function(obj={ctime:"",addres:""}){
    console.log( this.data.selectaddres,"傳入選擇地址")
   
    wx.request({
      url: schoolsearch ,//自己的服務(wù)接口地址
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        page:1,
        ctime:this.data.selectctime,
        addres:this.data.selectaddres
      },
      success: (res) => {
        console.log(res,"shoole")
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  onLoad: function (options) {
 
    //查詢學(xué)校
    this.schoolList()
  },

})

圖片描述
大概的意思就是我封裝了一個(gè)查詢學(xué)校列表的接口叫schoolList。
我的選擇學(xué)校的上課地址bindaddresChange方法的時(shí)候,我第一步已經(jīng)修改了selectaddres,并且答應(yīng)出來了,我是剛剛選擇的地址??墒窃谙旅嬲{(diào)用schoolList方法進(jìn)行篩選的時(shí)候,selectaddres這個(gè)數(shù)值,居然不是我剛剛選擇中的,我知道這this.setData是異步的問題。在網(wǎng)上查詢可以在this.setData({userInfo: userInfo}, callback)寫回調(diào)函數(shù),可是還是沒生效,有人遇到過這樣的問題么?

回答
編輯回答
笨尐豬

selectaddres: this.data.addres[e.detail.value]

2018年5月31日 15:03