【发布时间】:2020-11-03 23:28:35
【问题描述】:
如果数字大于 0,我只是尝试添加一个类,如果它小于 0,则添加另一个类。
这是我的代码:
var prices = new Vue({
el: "#prices",
data: {
message: "Hello Vue!",
currencies: [],
},
computed: {
color() {
return this.price_change_percentage_1h_in_currency > 0 ? "inc" : "dec";
}
},
// Getting the Data DO NOT TOUCH! :)
mounted: function() {
axios.get("https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=1h%2C%2024h%2C7d")
.then(response => {
this.currencies = response.data;
console.log(response);
})
.catch(error => {
console.log(error);
});
},
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<table id="prices">
<tbody>
<tr v-for="currency, index in currencies">
<td v-html="index + 1"></td>
<td :class="color">{{currency.price_change_percentage_1h_in_currency.toFixed(2)}}%</td>
<td :class="color">{{currency.price_change_percentage_24h.toFixed(2)}}%</td>
<td :class="color">{{currency.price_change_percentage_7d_in_currency.toFixed(2)}}%</td>
</tbody>
</table>
如您所见,我正在使用计算:color()。一切正常,但它总是将类“dec”添加到表数据中,即使它大于 0。
请帮助我结束我的痛苦。
谢谢。
【问题讨论】:
标签: javascript json vue.js