Vue.js middle [system]TypeError: Cannot read property 'push' of undefined

I have a problem when I write code in uni-app [system]TypeError: Cannot read property 'push' of undefined

		data() {
			return {
				date: this.getDate(),
				kind: ['养殖物异常', '设备异常', '偷盗', '野生动物', '灾害', '其他异常'],
				kindIndex: 0,
				detail: {},
				done: false,
				imageNames:[]
			}
		},
			loadImage(){
				uni.chooseImage({
					success: function(response){
						for(let file of response.tempFilePaths){
						let imageName=file.substring(file.lastIndexOf('/')+1);
						this.imageNames.push(imageName);		
						}
					}
				})
			}

Obviously it is an array but there is no push function. Why?

The reason is that this no longer points to the global object, but to the function, and the arrow function can continue to use the global this

			loadImage(){
				uni.chooseImage({
					success: (response)=>{
						for(let file of response.tempFilePaths){
						let imageName=file.substring(file.lastIndexOf('/')+1);
						this.imageNames.push(imageName);		
						}
					}
				})
			}

Guess you like

Origin blog.csdn.net/weixin_62264287/article/details/132258984