1.问题
在学习uniapp的过程中,通过uni.chooseImage()来上传本地图片。
原代码如下:
data(){
return {
imgArr:[]
}
},
methods:{
chooseImg(){
uni.chooseImage({
count:5,
success:(res){
this.imgArr = res.tempFilePaths
}
})
}
}
目的是为了获取本地上传图片的地址,然后将其赋值给imgArr。
然而这里的this是获取不到img。
2.解决办法
- 方法1
success:res=>{
this.imgArr = res.tempFilePaths
}
- 方法2
chooseImg(){
var _this = this;
uni.chooseImage({
count:5,
success:res=>{
_this.imgArr = res.tempFilePaths
}
})
}
个人认为是在success函数中this指向了uni这个对象,所以获取不到data中的数据,然后箭头函数是继承上一级的this指向,所以不会有原来的问题。
如果有理解错的地方还望告知。