zhuce: function () {
let that = this;
if (that.data.username.length == 0 || that.data.password.length == 0 || that.data.password == "" || that.data.password == "") {
wx.showModal({
title: '账号或密码不能为空!',
})
} else if (that.data.username.length < 10) {
wx.showModal({
title: '账号格式不正确!',
})
} else if (that.data.password.length < 6) {
wx.showModal({
title: '密码位数不能少于6位!',
})
} else {
console.log("Hello,用户:" + this.data.username + "\npassword:" + this.data.password);
wx.request({
url: 'http://localhost:8088/demo/User/ListByStuNo',
method: 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
'stuNo': this.data.username,
},
success: function (res) {
console.log("回调函数:" + res.data);
var ResData = res.data;
if (ResData == true) {
wx.showModal({
title: '用户已存在!',
duration: 2000
})
} else {
wx.request({
url: 'http://localhost:8088/demo/User/register',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
'stuNo': that.data.username,
'passWord': that.data.password
},
success: function (res) {
console.log("回调函数:" + res.data);
var ResData = res.data;
if (ResData == true) {
wx.showModal({
title: '注册成功!',
duration: 2000
})
wx.redirectTo({
url: '../denglu/denglu',
})
} else {
wx.showModal({
title: '注册失败!',
duration: 2000
})
}
}
})
}
}
})
}
},
这里的success回调中是闭包的,我的第二个获取this.data.username显示data未定义,就是调用不成功!
请问大佬们我该如何做才能保证我的第二个也能用上data里的数据
zhuce()函数用这个写
两种方式,一种把this改为that,通过作用域链的原理将函数内的this全都指向你在函数开头的let that = this,所赋值的this。第二种就用箭头函数了。建议有空补补JS基础。
你自己代码里都写了“let that = this;”
那相关原理应该是清楚的啊。