Code:-
<!--: is not working-->
<div : >
<div
v-if="props.currency"
:
>
{{ $options.methods.format(props.value) }}
</div>
<div
v-else-if="props.millifyOnly"
:
>
{{ $options.methods.millifyNumber(props.value) }}
</div>
<div
v-else
:
>
{{ 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>
Here the line :
is not working, the computed property is never called. The binding class doesn't work. I don't know why it is not being called, I have clearly defined it. I think :
is the correct way to binding a computed property with a class. I don't understand what exactly am I doing wrong here.
I have tried to even display my computed property like <p>HELLP {{props.title}} {{changeCalmarColor}}</p>
but it is never called. I cannot see it in the console.
CodePudding user response:
To access props you should use directly this.propName
. You could change your computed prop to:
changeCalmarColor() {
console.log("/////VALUE AND TITLE////", this.title, this.title);
if(this.title == "Calmar Ratio") {
if(this.value < 1 || this.value == null) {
return "text-danger";
} else if(1 <= this.value && this.value <= 3.00) {
return "text-yellow";
} else if(1 <= this.value && this.value > 3.00) {
return "text-green";
}
}
}
CodePudding user response:
A computed property gets cached, See docs
So changeCalmarColor
runs once and then waits for dependencies to change in order to re-run again.
The reason why it doesn't run is because you have used this.props.title
while you should use this.title
.
So this:
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";
}
}
},
Should be changed to:
changeCalmarColor() {
if(this.title == "Calmar Ratio") {
if(this.value < 1 || this.value == null) {
return "text-danger";
} else if(1 <= this.value && this.value <= 3.00) {
return "text-yellow";
} else if(1 <= this.value && this.value > 3.00) {
return "text-green";
}
}
},