Using Filters in Vue
Frontend Engineer who loves building tools.
Search for a command to run...
Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transiti...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

Filters in Vue.js are helper functions that can be used to quickly format text in a component's template.
Filters can be used in two places in a template:
v-bind expression.<!-- usage in interpolations -->
<p> {{ amount | round }} </p>
<!-- in v-bind -->
<input type="text" v-bind:value="amount | round" disabled/>
To use a filter, we simply add the name of the function to the end of the expression which is signified by the "pipe" symbol. (|).
According to[ Linfo.org](http://www.linfo.org/pipes.html "All about pipes")
A pipe is a form of redirection that is used to send the output of one program to another program for further processing.
So in this case, the value from the first expression is now sent via the "pipe" to the filter function to return the new value.
They can either be registered globally for use in multiple components or locally in a single component.
We can set the filter function for use in multiple components by setting it using Vue.filter() just before initialization:
import Vue from 'vue';
Vue.filter('round', function(value){
if(!value) return '';
return (typeof value === 'number')? value.toFixed(2) : value;
})
Now the filter can be used in any component.
To use a filter locally, simply add it to the filters object as a method:
filters: {
round(value){
if(!value) return '';
return (typeof value === 'number')? value.toFixed(2) : value;
}
}
The value from the first expression is always the first argument, It's possible to pass arguments to a filter function.
If we wanted to tweak the outcome of the filter, say to modify how many decimal places the round function returns.
In the template, we simply add the arguments like a normal function:
<p> {{ amount | round(3) }} </p>
And in the filter, we add a new parameter like so:
filters: {
round(value, decimalPlace = 2){
if(!value) return '';
return (typeof value === 'number')? value.toFixed(decimalPlace ) : value;
}
}
We can pass the value to another filter too after the first modified value is being returned.
<p> {{ amount | round(3) | currency('$') }} </p>
This way, we could get an even refined value.
filters: {
round(value, decimalPlace = 2){
if(!value) return '';
return (typeof value === 'number')? value.toFixed(decimalPlace ) : value;
},
currency(amount, symbol){
return `${symbol} ${amount}`
}
}