Multiple usage of custom filters in Vue.js

Best way to use filters in Vue.js

A Vue.js filter is simply a function that takes an input, processes it and gives an output. Inside a Vue template it is written after a single pipe and can also be followed by its arguments.

Example of Vue.js filter

But there can be some particular case where the filter functions are required to be used outside directive and mustaches. This article will show a simple mechanism to tackle the problem.

A custom filter can be registered either globally or inside components. Since we are doing it the best way, we will be registering it globally.

If you have many filters, its best to keep it on a separate folder inside separate files. For this particular case, I have kept it inside

place filters.js inside ‘src’

Now lets take a look inside filters.js file.


As you may have noticed, the object contains all of your filter functions. The key serves as and the value as

The file exports two things

  • object
  • function

That’s the main point. The function registers all of the filter functions globally so that it can be used on any Vue component. The method is called inside the file.

inside main.js

Another cool thing about this approach is the ability to import those filter functions and use inside JavaScript logic.

inside JavaScript logic of XXX.vue component

In many cases filter functions need to be used outside of directive and mustaches. This approach works best to handle this particular scenario. Moreover, it groups the filter functions at one place and makes a clear separation of logic inside vue application.

Like the code snippets used in this article. It’s generated using this awesome tool RamroCode 👌

Happy Coding !

Web Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store