vue v-for 使用問題整理


今天使用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操作放到一個臨時變量中,不然就會報錯喲。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM