Filters in Vue.js are a simple yet powerful feature that allows you to transform data before displaying it in the UI. They are most commonly used in templates to format or manipulate text, dates, numbers, and other data types.
Filters in Vue.js allow you to apply formatting or transformation to your data directly in the template. Filters are primarily used to modify or format text output without changing the underlying data.
Filters are typically used for simple text formatting tasks, such as:
{{ message | capitalize }}
In this example, the capitalize
filter would capitalize the first letter of the message
string.
In Vue.js, filters are applied using the pipe symbol (|
) in the template. Here’s the basic syntax for using a filter in a Vue.js template:
{{ expression | filterName }}
expression
is the data or variable you want to format.filterName
is the name of the filter you are applying.
{{ message | capitalize }}
Hello world
In this example, the capitalize
filter would capitalize the first letter of the message
string.
Vue.js provides the flexibility to create custom filters for more complex or specific transformations that are not available out of the box. Custom filters can be globally or locally registered in your Vue.js application.
Global filters are available throughout your application and can be defined using Vue.filter()
.
Vue.filter('capitalize', function(value) {
if (!value) return '';
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1);
});
Vue.filter()
: Registers a global filter.value
: The input data passed to the filter.Now, you can use this filter in any component:
{{ message | capitalize }}
Hello world
Filters can also be registered locally within a specific Vue component. Local filters are only available within that component.
{{ message | capitalize }}
Vue.js filters
In this example, the capitalize
filter is only available within the current component.
Vue.js allows you to chain multiple filters together, which means you can pass the output of one filter into another. This can be helpful when you need to apply several transformations to the same data.
{{ message | capitalize | truncate(10) }}
message
data is first passed through the capitalize
filter and then through the truncate
filter, which limits the string length to 10 characters.Let’s define the truncate
filter:
Vue.filter('truncate', function(value, limit) {
if (!value) return '';
if (value.length > limit) {
return value.substring(0, limit) + '...';
}
return value;
});
Now, combining both filters:
{{ message | capitalize | truncate(10) }}
Vue.js cus...
You can also pass arguments to filters in Vue.js to make them more flexible. Filters can accept parameters, which can be used to alter the behavior of the filter.
{{ price | currency('USD') }}
currency
filter adds a currency symbol to the number.parseFloat(value).toFixed(2)
ensures the number is formatted to two decimal places.
USD100.00
While working with filters, it’s important to handle edge cases, such as null
or undefined
values. If the input data is not properly validated, it can lead to bugs or crashes.
Vue.filter('capitalize', function(value) {
if (!value) return '';
return value.charAt(0).toUpperCase() + value.slice(1);
});
In this case, the filter checks if the value
is null
or undefined
and returns an empty string to prevent errors.
While filters are most commonly used in templates, you can also use them within Vue.js directives like v-bind
and v-model
.
{{ message | capitalize }}
As you type into the input field, the value will be capitalized in real time.
Though filters are primarily designed for template usage, similar logic can be applied within component methods if you need more control over data transformation.
{{ formattedMessage }}
In this example, the filter logic is moved to a method and applied programmatically.
null
, undefined
, or unexpected data types to avoid runtime errors.As of Vue 3, filters have been deprecated in favor of computed properties or methods. Though filters are still useful in Vue 2, Vue 3 encourages the use of more flexible and maintainable approaches like methods or computed properties.
Vue.js filters are a useful and straightforward feature that allows you to format and transform data within your templates. Whether you're formatting dates, capitalizing strings, or formatting numbers, filters provide a clean, reusable way to handle these transformations. Happy Coding!❤️