0

I want to change the value of a boolean after some time and keep repeating the process. Then I want to pass the value to a child component individually. Here changeActive() is the function to change the value of the active boolean. I want to change the value to the first prop then after some time second prop and so on.

<template>
<div style="width:300px; margin: auto;">
      <RatingLabel 
      :rating='rating[0]'
      :active='active'
      style="margin: auto;"
      />
      <RatingLabel 
      :rating='rating[1]'
      :active='active'
      style="float: right;"
      />
      <RatingLabel 
      :rating='rating[2]'
      :active='active'
      />
      <RatingLabel 
      :rating='rating[3]'
      :active='active'
      style="margin: auto;"
      />
</div>
</template>

<script>
import RatingLabel from '../atomic/RatingLabel'
import { mapState } from 'vuex'
export default {
      components: {
            RatingLabel,
      },

      data() {
            return {
                  active: false,
            }
      },

      methods: {
            changeActive() {
                  setTimeout(function(){ 
                        this.active = !this.active;
                        console.log(this.active)
                   }, 3000);
            }
      },

      mounted() {
            this.changeActive()
      },

      computed: mapState(['rating'])
}
</script>
4

1 回答 1

1

这是您需要做的:

  1. 不要重复该组件,而是添加一个v-for
<RatingLabel v-for="(rating,i) in ratings" :key="i" :rating="rating"/>
  1. 由于每个 prop 是独立的,因此需要制作active一个列表,并根据索引将每个元素作为 prop 传递,如下所示:

将其添加data

activeList: [],

所以组件渲染将是:

<RatingLabel v-for="(rating,i) in ratings" :key="i" :active="activeList[i] || false" :rating="rating"/>
  1. 由于您需要在每个prop时间段将 each 设置为 true,因此您需要setInterval功能:

将此添加到data

time:''

然后将其用作间隔:

mounted() {
    this.changeActive();
},
methods: {
     changeActive: function() {
          let count = 0;
          var x = setInterval(() => {      
               this.activeList.push(true);
               count++;
               if (count == this.ratings.length) clearInterval(x);  
          }, 3000);        
     },
},
于 2020-06-14T11:35:49.093 回答