wx.request的success中无发修改页面的初始数据?

小程序 文章 2021-04-05 10:42 550 0 全屏看文

AI助手支持GPT4.0

wx.request的success中无发修改页面的初始数据?Didn’t modify the initial data of the page in the success of wx.request?

wx.request的success中无发修改页面的初始数据?
下面两个方法都无法修改初始化数据中的sms,第一种是没效果,第二种直接报sms_code为空。求各位前辈指点一下//反法一不行Page({  /**   * 页面的初始数据   */  data: {    TabCur0,    scrollLeft:0,    type0,    btn_disabled:true,    sms0  },  onLoadfunction (options{    wx.request({      url'http://xxx/wx/login/config',      successfunction(res){          this.setData({            sms:res.data.data          })      }    })  },//方法二也不行Page({  /**   * 页面的初始数据   */  data: {    TabCur0,    scrollLeft:0,    type0,    btn_disabled:true,    sms0  },  onLoadfunction (options{            var sms_code;        wx.request({          url'http://xxx/wx/login/config',          successfunction(res){          sms_code = res.data.data;            }          })           this.setData({            sms: sms_code       })              },

The following two methods cannot modify SMS in initialization data. The first method is ineffective, and the second method reports SMS directly_ Code is empty. Please give me some advice / / page ({/ * * * initial data of page * / data: {tabcur: 0, s crollLeft:0 ,    type: 0,    btn_ disabled:true ,    sms: 0  },  onLoad: function (options) {     wx.request ({      url: ' http://xxx/wx/login/config ',      success: function(res){           this.setData ({             sms:res.data.data           })      }    })  }, //Method 2 is not good either. Page ({/ * * * initial data of page * / data: {tabcur: 0, s) crollLeft:0 ,    type: 0,    btn_ disabled:true ,    sms: 0  },  onLoad: function (options) { var sms_ code;      wx.request ({       url: ' http://xxx/wx/login/config ',       success: function(res){ sms_ code = res.data.data ;       }     })   this.setData ({         sms: sms_ code    })   },

回答:

 :
//作用域的问题,最外层加上var that = this:var that = thiswx.request({  url'http://xxx/wx/login/config',  successfunction(res){    that.setData({      sms:res.data.data    })  }})
Made in.内江:

this指向没找到:

onLoad(options) {   wx.request({    url: 'http://xxx/wx/login/config',    success: (res) => {     let sms = res.data.data     this.setData({       sms     })    }  })}
|G.XIAO|:

如果支持 es6 的话 ,改成箭头函数

success: (res) => {  // 这里 this 能正常使用}


詹迪克:

方法一 this 作用域问题

方法二 异步问题

青寒:

在方法外面,var that=this; 然后在方法的success里面用that.setData设置值。

-EOF-

AI助手支持GPT4.0