This tutorial is out of date and no longer maintained.
Similar to AngularJS, Vue.js has its way of transforming data and applying filters to it, but you must keep in mind that filters don’t transform the original data, they only change the output and return a filtered version of it. Filters can be useful in a lot of different situations like keeping your API responses as clean as possible and handling the formatting of your data on your front end. They can also be efficient in cases where you want to avoid repetition and concatenation by encapsulating all that logic behind reusable code blocks.
I hope this small introduction got you excited to learn more about Filters. How you can create and use them and couple more things you will discover going through the article.
If this isn’t your first time reading about Vue.js filters then you know that the older versions shipped with built-in filters, but they got removed from Vue 2.0 and this is Evan You’s (the creator of Vue.js) reasoning behind that:
Built-in filters can be useful, but they lack the flexibility of pure JavaScript. When a built-in function doesn’t suit your needs, you either end up re-implementing something similar (and shipping both in your final code, where the built-in becomes useless, dead code) or have to wait for Vue to update them and release a new version.
With that in mind, be careful reading or watching old tutorials. Here is a full list of the old default filters in case you want to learn more about them: Filters - vue.js.
We will be reproducing a few of them in the examples below.
With Vue, you can register your filters in two different ways: Globally and Locally. The former gives you access to your filter across all your components, unlike the latter which only allows you to use your filter inside the component it was defined in.
Filters are simple JavaScript functions, they take the value to be transformed as the first parameter, but you can also pass in as many other arguments as you will need to return the formatted version of that value.
Here is what a Global filter looks like:
The filter definition must always be above the main Vue instance, or you will get a “Failed to resolve filter: toUSD”
error.
Local filters are registered to a Vue component scope. The following illustrates how they are created:
As you can see in the example above, Local Filters are stored within the Vue component as functions inside the “filters” property. You can register as many as you want:
As we mentioned in the introduction of this article, Filters can take as many arguments as you need:
In this example, we created a filter with the name “readMore” which will limit the length of a string to a given number of characters and will also append a suffix of your choice to it. Vue.js passes the value to be filtered as the first param text and length, suffix as the second and third parameter.
See it in action: Edit fiddle - JSFiddle
One of my favorite things about Filters is the ability to chain them using the pipe ( |
) symbol and run a single value through a series of transformers.
Let’s use the example of price value again; we want to limit it to two numbers after a comma ( ,
) and add the dollar sign ( $
) to it.
Although we can achieve this using one single filter we might also want to use toUSD
filter on its own. Separating and chaining filters, in this case, is the way to go:
If you made it to this part of the article, congratulations! That was everything you needed to know about Vue.js filters, but it is always a good idea to go through few examples:
If you are interested in these filters and you want to use them in your project GitHub - wy-ei/vue-filter: A collection of Vue.js filter. offers a set of very useful Vue.js filters including the ones above. Head over there to see how you can install and use them.
I hope you learned something from this post and you now know how to create and use filters and most importantly you can now refactor your code and clean it a bit with filters.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!