Skip to content

TypeError: Cannot read properties of undefined (reading 'length')

【问题】在通过数组 monthCourseHours 渲染页面时,加上了 length 做判断,但是却报了错误:

bash
{{ monthCourseHours.length > 0 ? monthCourseHours.after_change : 0}}

在这里插入图片描述

bash
[Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'length')"

在这里插入图片描述

【解决】再在length前加一级判断undefined

bash
 {{ totalCourseHours !== undefined && monthCourseHours.length > 0 ? monthCourseHours.after_change : 0}}

在这里插入图片描述

如果解决不了,就使用下面的方式:

bash
{{ monthCourseHours && monthCourseHours.after_change ? monthCourseHours.after_change : '0' }}

在这里插入图片描述

编程洪同学服务平台是一个广泛收集编程相关内容和资源,旨在满足编程爱好者和专业开发人员的需求的网站。无论您是初学者还是经验丰富的开发者,都可以在这里找到有用的信息和资料,我们将助您提升编程技能和知识。
专业开发
高端定制
售后无忧
站内资源均为本站制作或收集于互联网等平台,如有侵权,请第一时间联系本站,敬请谅解!本站资源仅限于学习与参考,严禁用于各种非法活动,否则后果自行负责,本站概不承担!