vue v-for the use of finishing

Today, the use of v-for instruction when encountered an error

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

Using the code page

            <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>

The reason given:

  I guess the reasons for using nested property, can not resolve a specific attribute values ​​in the page, this is the reason I tried out, but do not know the deeper reason, and knowing there is hope for comment.

solution:

  Since we know the reason, it is like solved, the solution is as follows.

            <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>

 Variable transit through it, into another temporary variable, if there are nested reference attributes, then, we remember through js operation into a temporary variable, otherwise it will error yo.

Guess you like

Origin www.cnblogs.com/dawenyang/p/11275552.html