uniapp箭头函数

文章描述:

uniapp箭头函数怎么使用?

文档

https://www.kancloud.cn/zengqs1976/uni-app/1144477

 

success成功返回作为示例:

template

<view class="" @tap="sendImg('album')">图片</view>

不使用箭头函数,需要定义 _this = this

script

var _this;
export default{
	onReady() {
		_this = this
	},
	methods:{
		sendImg(e){
			let count = 9;
			if(e == 'album'){
				count = 9;
			}else{
				count = 1;
			}
			
			uni.chooseImage({
				count:count,	// 默认9
				sizeType:['original','compressed'],
				sourceType:[e],	// 从相册选择
				success:function(res){
					// console.log(JSON.stringify([res.tempFilePaths]))
					const filePaths = res.tempFilePaths
					for(let i = 0; i<filePaths.length; i++){
						_this.send(filePaths[i],1)
					}
				}
			})
		},
		send(msg,types){
			console.log(msg,types)
		}
	}
}

使用箭头函数(res)=>{}

export default{
	onReady() {
		
	},
	methods:{
		sendImg(e){
			let count = 9;
			if(e == 'album'){
				count = 9;
			}else{
				count = 1;
			}
			
			uni.chooseImage({
				count:count,	// 默认9
				sizeType:['original','compressed'],
				sourceType:[e],	// 从相册选择
				success:(res)=>{
				
				    const filePaths = res.tempFilePaths
				    for(let i = 0; i<filePaths.length; i++){
				        this.send(filePaths[i],1)
				    }
				}
			})
		},
		send(msg,types){
			console.log(msg,types)
		}
	}
}

 

发布时间:2022/09/26

发表评论