How to change data correctly during vue Recursion

method 1:

in the parent component of a recursive component:

<plan-preview :sectionList="sectionList"></plan-preview>

Recursive component:

<plan-section-list v-if="section.children" :sectionList="section.children"></plan-section-list>
  name: "planSectionList",
  props: {
    sectionList: Array
  },
  mounted() {
    _.forEach(this.sectionList, async section => {
      const res = await this.$api.plan.getSectionDetails(section.id);
      this.$set(section, "details", res.data.data);
    });
  }

method 2:

in the parent component of a recursive component:

<plan-preview :sectionList.sync="sectionList"></plan-preview>

Recursive component:

<plan-section-list v-if="section.children" :sectionList.sync="section.children"></plan-section-list>
  name: "planSectionList",
  props: {
    sectionList: Array
  },
  mounted() {
    _.forEach(this.sectionList, async section => {
      const res = await this.$api.plan.getSectionDetails(section.id);
      this.$set(section, "details", res.data.data);
    });
    this.$emit("update:sectionList", this.sectionList);
  }

both ways update the data, but the view is not updated. Why?

Update:

suddenly discovered the factors that caused the view not to be updated. The following is the recursive component code:

<template>
  <ul class="plan-preview__section-list">
    <li v-for="section in sectionList" :key="section.id">
      <div class="plan-preview__section" @click="section.show = !section.show">
        <h4 class="plan-preview-section__title">{{section.title}}</h4>
        <div v-show="section.show" class="plan-preview-section__body">
          <p class="plan-preview-section__content">{{section.content}}

:{{section.details}}

</div> </div> <plan-section-list v-if="section.children" :sectionList.sync="section.children"></plan-section-list> </li> </ul> </template>

where section.details is used to test whether the view is updated. Its parent element vshow = "section.show" is a big hole, and it"s perfectly normal to delete it. Why is that?

Apr.19,2021

has made a big own. sectionList is also obtained asynchronously in the parent component of the recursive component. Before the sectionList data is obtained, the recursive component has already mounted. At this time, the recursive component gets a sectionList of zero length and does not initiate a request for details, let alone details data. Then it doesn't really matter about v-show , it's just careless. The sectionList data obtained by the parent component is a nested tree data. I traverse the parent component once and only set the show attribute to the first layer of data. The deeper structure has no show attribute, so the judgment of v-show does not take effect (the deep details data has been obtained), but it will not be displayed all the time. Seeing that there is no data on the first floor, I mistakenly think that there is no data in the deep.

Menu