vue v-for 使用问题Error in render

摘要:今天使用v-for指令的时候遇到一个错误:[Vue warn]: Error in render: \\\"TypeError: Cannot read property \\\'children\\\' of undefined\\\",猜测使用了嵌套属性的原因,在页面中无法解析出具体属性值

今天使用v-for指令的时候遇到一个错误

[Vue warn]: Error in render: "TypeError: Cannot read property 'children' of undefined"


页面使用代码

<template v-for="(c,i) in modelList.Course.children">
  <div :key="i" class="course-block">
    <CourseStruct :process="isbuy" :course="c" />
  </div>
</template>
<script>

export default {
        methods: {
            async getList(id) {
                const res = await GetChapterListByProductID(id);
                if (res.data) {
                    this.modelList = res.data;
                 }
            }
      }
}

</script>


报错原因:

我猜测使用了嵌套属性的原因,在页面中无法解析出具体属性值,这个原因是我尝试出来的,但是不知道深层次的原因了。


解决方案:

既然知道了原因,那么就好解决了,解决方法如下.

<template v-for="(c,i) in cls">
	<div :key="i" class="course-block">
		<CourseStruct :process="isbuy" :course="c" />
	</div>
</template>

<script>
	export default {
		methods: {   
			async getList(id) {  
				const res = await GetChapterListByProductID(id);  
				if(res.data) {
					this.modelList = res.data;
					var co = this.modelList.Course
					this.cls = co.children  
				}   
			}    
		}      
	}
</script>

 通过变量中转一下,放到另一个临时变量中,如果有嵌套引用属性的话,大家记得通过js操作放到一个临时变量中,不然就会报错。


本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_4419