今天使用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操作放到一個臨時變量中,不然就會報錯喲。
總結
以上所述是小編給大家介紹的vue v-for 使用問題整理小結,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答