Unhandled error during execution of native event handler ;Cannot read property ‘children of undefine

文章讲述了在Vue应用中遇到的两种错误:执行本机事件处理程序时的未处理错误和尝试访问未定义的children属性。通过在代码中添加更严谨的条件判断来避免直接访问可能不存在的属性,从而解决了问题。原始的错误处理方法try-catch只能捕获错误,但并未解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:问题分析

数据能出来,页面正常显示,但是有报错。

[Vue warn]: Unhandled error during execution of native event handler ;意思是执行本机事件处理程序期间出现未处理的错误

TypeError: Cannot read property 'children' of undefined 意思是属性 children未定义

报错处代码:而且点击错误直接定位到了this.categoryList[index]这里得index后面,由此可知是这里有问题

clickFirstCategory(index) {
	this.isActive = index;
				// 点击过快时,商品展示报错(找不到children)
	this.secondCateList = this.categoryList.length > 0 && this.categoryList[index].children;
	this.thirdCateList = this.secondCateList.length > 0 && this.secondCateList[index].children;
}

 2.解决

既然是执行本机事件处理程序期间出现未处理的错误,那么只要处理了这个错误就行。

首先想到得就是try catch,但是虽然错误捕捉到了,但是如果打印catch里的错误日志,还是存在,所以其实只是捕捉了错误,并没有解决

解决办法:改写此处判断条件,问题解决

if (this.categoryList.length > 0 && this.categoryList[index])
	this.secondCateList = this.categoryList[index].children;
if (this.secondCateList.length > 0 && this.secondCateList[index])
	this.thirdCateList = this.secondCateList[index].children;

参考:2021-11-22 VUE3控制台出现'Unhandled error during execution of component event handler'警告解决方法 - 码农教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值