為啥我的賦值不能賦值到deat里面去,這是為什么。為呀為什么。咦,咦!
網(wǎng)友回復(fù):
將this改為that,然后在方法開始加入 const that = this
熊貓,是改哪一個this,,,,,,,(- _-)
閉包
樓上正解,在request的success函數(shù)里,this的上下文已經(jīng)改變了
你可以幫我打一下嗎。小熊貓。,,,非常感謝
onLoad: function (options) {
var rs = [];
wx.request({
url: ',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res.data.showapi_res_body.pagebean.songlist);
rs = res.data.showapi_res_body.pagebean.songlist;
this.setsj(rs);
}
});
},
setsj: function (shuju) {
console.log(shuju);
this.setData({
recommends: shuju
});
啥子是閉包
每個function的第一行都寫上const that=this,然后里面凡是用到this的地方全部改成that,就oK了。
onLoad: function (options) {
const that = this
var rs = [];
wx.request({
url: ',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res.data.showapi_res_body.pagebean.songlist);
rs = res.data.showapi_res_body.pagebean.songlist;
that.setsj(rs);
}
});
},
setsj: function (shuju) {
console.log(shuju);
this.setData({
recommends: shuju
});
你們說的,咋個我有點亂呢
按 YJFn 的寫法就行了