最近学习了uniapp,在使用uni.request时使用success设置,元素值时发生错误。此时必须使用箭头表达式。
当我们使用箭头函数的时候,箭头函数会默认帮我们绑定外层 this 的值,所以在箭头函数中 this 的值和外层的 this 是一样的。
以下原代码
get(){
uni.request({
url:"请求接口",
success(res){
this.url=res.data.url
}
})}
这样写无法给url赋值,必须使用箭头表达式,如下
get(){
uni.request({
url:"请求地址",
success :((res)=>{
this.url=res.data.url
}
)
})},
实际上,对于ajax请求一般可以封装,但是我还没有熟悉环境,所以暂时使用原始的方法。
箭头表达式大概使用方法是:
function(x, y) {
return x * y;
}
等价于
(x, y) =>{ x * y;}