先上一段代碼:
export default {
data() {
return {
token:''
}
}
}
let that=this
uni.request({
url:this.$BaseUrl+'/CompanyController/establishToken',
methods:'POST',
header:{
'Content-type':'application/x-www-form-urlencoded'
},
dataType:"json",
success:funtion(){
// this.token=res.data.token;
that.token=res.data.token;
}
})
這個代碼中使用普通的success:funtion(){} 經常用普通函數 因此我們使用this.token賦值的時候出現怎麼都賦不上值因爲
uni.request 中uccess方法指向閉包,所以this屬於閉包,由此在success回調函數裏是不能直接使用this.tokne的 可以在方法外使用let that=this的方法 that.token=res.data.token ;
當然還有另外一種方法使用使用箭頭函數直接使用this指向就可以
uni.request({
url:this.$BaseUrl+'/CompanyController/establishToken',
methods:'POST',
header:{
'Content-type':'application/x-www-form-urlencoded'
},
dataType:"json",
success:(res)=>{
this.token=res.data.token;
}
})
各位小姐姐,小哥哥 小編如果能夠給各位解決問題,留下愛心,足跡哦