I have a simple component to display cryptocurrencies and I want to update the amount every second (and reflect the updated value). I can display the amount but my function to add 1 every second is not working. I tested with something else like alert()
and that was working. Is it my selector?
<template>
<div>
<ul class="crypto-list">
<li v-for="(currency, idx) in currencies" :key="idx" class="currency">
<span class="currency__acronym">{{ currency.acronym}} </span>
<h2 class="currency__name">{{ currency.name }}</h2>
<span class="currency__amount"> {{ currency.amount }} </span>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'Cryptocurrency',
data() {
return {
currencies: [{
name: 'Ethereum',
acronym: 'ETH',
amount: 1,
},{
name: 'ICON',
acronym: 'ICX',
amount: 40,
},{
name: 'Nano',
acronym: 'XRB',
amount: 14,
}],
};
},
mounted () {
this.addNumber();
},
methods: {
addNumber: function() {
setInterval(function() {
this.currencies[0].amount += 1;
this.currencies[1].amount += 1;
this.currencies[2].amount += 1;
}, 1000);
},
},
};
</script>