我想在一段时间后更改布尔值,并继续重复该过程。然后,我想将值分别传递给子组件。这里的 changeActive()是用于更改 active 布尔值的函数。我想将值更改为第一个属性,然后在一段时间后更改为第二个属性,依此类推。
<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>
答案 0 :(得分:1)
这是您需要做的:
v-for
:<RatingLabel v-for="(rating,i) in ratings" :key="i" :rating="rating"/>
active
列为列表,并根据索引将每个元素作为道具传递,如下所示:将其添加到data
activeList: [],
因此,组件渲染应为:
<RatingLabel v-for="(rating,i) in ratings" :key="i" :active="activeList[i] || false" :rating="rating"/>
prop
设置为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);
},
},