【发布时间】:2021-12-17 16:55:37
【问题描述】:
代码:-
<!--:class="changeCalmarColor" is not working-->
<div :class="changeCalmarColor" class="content" >
<div
v-if="props.currency"
:class="[
'font-bold',
{ 'text-3xl': props.largeValue, 'text-center': props.center },
]"
>
{{ $options.methods.format(props.value) }}
</div>
<div
v-else-if="props.millifyOnly"
:class="[
'font-bold',
{ 'text-3xl': props.largeValue, 'text-center': props.center },
]"
>
{{ $options.methods.millifyNumber(props.value) }}
</div>
<div
v-else
:class="[
'font-bold',
{ 'text-3xl': props.largeValue, 'text-center': props.center },
]"
>
{{ props.value }}
</div>
</div>
<script>
import millify from "millify";
export default {
name: "StatCard",
props: {
type: {
type: String,
default: "normal",
},
icon: {
type: String,
},
iconPack: {
type: String,
default: "",
},
title: {
type: String,
required: true,
},
value: {
type: [String, Number],
required: true,
},
currency: {
type: Boolean,
},
millifyOnly: {
type: Boolean,
},
largeValue: {
type: Boolean,
default: true,
},
center: {
type: Boolean,
default: true,
},
},
methods: {
format(val) {
let millifiedVal = "";
if (!isNaN(parseFloat(val))) {
if (parseInt(val).toString().length > 3)
millifiedVal = millify(val, { precision: 2 });
else millifiedVal = parseFloat(val).toFixed(2);
if (millifiedVal.charAt(0) === "-") return `-$${millifiedVal.slice(1)}`;
else return `$${millifiedVal}`;
}
return val;
},
millifyNumber(val) {
return !isNaN(parseFloat(val)) ? millify(val, { precision: 2 }) : val;
},
},
computed: {
changeCalmarColor() {
console.log("/////VALUE AND TITLE////", this.props.title, this.props.title);
if(this.props.title == "Calmar Ratio") {
if(this.props.value < 1 || this.props.value == null) {
return "text-danger";
} else if(1 <= this.props.value && this.props.value <= 3.00) {
return "text-yellow";
} else if(1 <= this.props.value && this.props.value > 3.00) {
return "text-green";
}
}
},
},
};
</script>
这里:class="changeCalmarColor" 行不工作,计算的属性永远不会被调用。绑定类不起作用。我不知道为什么它没有被调用,我已经明确定义了它。我认为:class="changeCalmarColor" 是将计算属性与类绑定的正确方法。我不明白我到底在做什么错。
我什至尝试显示我的计算属性,如<p>HELLP {{props.title}} {{changeCalmarColor}}</p>,但它从未被调用。我在控制台中看不到它。
【问题讨论】:
标签: vue.js vuejs2 vue-component vuetify.js computed-properties