To use Vue.js filters to format data, you can follow these steps:
filters: {
formatCurrency(value) {
return '$' + value.toFixed(2);
}
}
{{ myNumber | formatCurrency }}
.Here's an example of using the formatCurrency
filter in a Vue component:
<template>
<div>
<p>{{ originalPrice }}</p>
<p>{{ originalPrice | formatCurrency }}</p>
</div>
</template>
<script>
export default {
data() {
return {
originalPrice: 10.5
};
},
filters: {
formatCurrency(value) {
return '$' + value.toFixed(2);
}
}
};
</script>
In the above example, the originalPrice
data property is initially set to 10.5. In the template, the first <p>
tag will display the original value (10.5), while the second <p>
tag will display the formatted value ('$10.50') using the formatCurrency
filter.
By using Vue.js filters, you can easily format data in your templates without modifying the source data itself.