前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue.js中的[system]TypeError: Cannot read property ‘push‘ of undefined

Vue.js中的[system]TypeError: Cannot read property ‘push‘ of undefined

作者头像
叶茂林
发布2023-08-14 09:48:18
1720
发布2023-08-14 09:48:18
举报
文章被收录于专栏:叶子的开发者社区

我在uni-app中写一下代码时出现问题[system]TypeError: Cannot read property 'push' of undefined

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

明明是数组却没有push功能这是为何

原因是此时的this不再指向全局对象,而是指向该函数,改用箭头函数可以继续使用全局的this

代码语言:javascript
复制
			loadImage(){
				uni.chooseImage({
					success: (response)=>{
						for(let file of response.tempFilePaths){
						let imageName=file.substring(file.lastIndexOf('/')+1);
						this.imageNames.push(imageName);		
						}
					}
				})
			}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档