The problem of passing values in vue Multi-layer components

Business background

< hr >

imitates the effect of snapping up progress bars in Pinduoduo, as follows

clipboard.png

  • merchandise list is a component goods , which renders a merchandise
  • in a loop.
  • the progress bar is a component progress that introduces
  • into the loop of the list of items.

difficulties encountered

< hr >
  • when rendering a component, the values printed in the mounted hook of the progress bar component are 30 90 , respectively.
  • after the final rendering is completed
  • the progress bar data of each item is taken from the progress bar data of the last item 90

sample code

< hr >
index.vue
<template>
  <goods :list.sync="list"></goods>
</template>
<script>
  import goods from "componects/goods";
  data () {
    return {
      list:[
        {name:"xx", progress: 30},
        {name:"bb", progress: 90},
      ]
    }
  }
  components: {
    goods
  }
  ...
</scrip>
goods.vue
<template>
  <div>
    <div v-for="item in list">
      ...
      <progress :number="item.progress"></progress>
      ...
    </div>
  </div>
</template>
<script>
  import progress from "./progress";
  props: {
      list: Array
  }
  components: {
    progress
  }
  ...
</scrip>
progress.vue
<template>
  <div :progress="progress">  </div>
</template>
<script>
  props: {
    progress: 0
  },
  mounted () {
    console.log(this.progress); // => 30,90
  }
</script>

question

< hr >
  • Why is there such a problem? (feels the same as the problem with closure references)
  • what is the solution?
Mar.15,2021

progress this component is written by yourself, can not be on the progress code, personal feeling that the problem lies in the instantiation of progress components, want to see the code.





your fault is that progress.vue didn't get props: ['number'] and assign values to progress

.

v-for loop without key? Try adding a key. I also guessed

Menu