使用this.setData报‘of undefind’,怎么解?

小程序 文章 2021-12-10 19:00 334 0 全屏看文

AI助手支持GPT4.0

使用this.setData报‘of undefind’,怎么解?How to use this.setData to report ‘of undefind’?

使用this.setData报‘of undefind’,怎么解?
const app = getApp()Page({    data: {      msg:"1234567890"    },    onLoad() {        wx.request({          url : 'http://127.0.0.1:7001/logined',          data : {              usname : "werewrr",              password : "12345678"          },          header: {            'content-type': 'application/json'           },          method : "POST",          dataType : 'json',          success(res){              var d = res.data.data              console.log(d)              this.setData({                msg: 'd[0].username'              })          }        })    }  })


const app = getApp()Page({    data: {      msg:"1234567890"    },    onLoad() {        wx.request({          url : 'http://127.0.0.1:7001/logined',          data : {              usname : "werewrr",              password : "12345678"          },          header: {            'content-type': 'application/json'           },          method : "POST",          dataType : 'json',          success(res){              var d = res.data.data              console.log(d)              this.setData({                msg: 'd[0].username'              })          }        })    }  })

回答:

圣殿骑士:

this的作用域不对

建议在wx.request 外面 声明 let self = this;

self.setData()

Future:

在wx.request外 声明let _this = this _this.setData({})

假装在上海:
msg: d[0].username


-EOF-

AI助手支持GPT4.0