Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
In Vue 3, filters are removed and are no longer part of the official API. Filters were a way to apply common text formatting in Vue 2, but you can easily replace them with methods, computed properties or even a plugin if needed.
Here's how you can replace filters with methods and computed properties:
Methods
Methods are a good replacement for filters that take arguments:
<template> <div>{{ formatName(user) }}</div> </template> <script> export default { data() { return { user: { firstName: 'John', lastName: 'Doe' } } }, methods: { formatName(user) { return `${user.lastName}, ${user.firstName}` } } } </script>
Computed Properties
Computed properties are the way to go for more complex formatting that depends on reactive data:
<template> <div>{{ fullName }}</div> </template> <script> import { ref, computed } from 'vue'; export default { setup() { const user = ref({ firstName: 'John', lastName: 'Doe' }); const fullName = computed(() => `${user.value.lastName}, ${user.value.firstName}`); return { fullName } } } </script>
Global Filters Replacement
In Vue 2, you could define global filters that would be available in all components. You can achieve similar functionality in Vue 3 by creating a global mixin and defining methods in it:
import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); app.mixin({ methods: { formatCurrency(value) { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value); } } }); app.mount('#app');
Then in any component:
<template> <div>{{ formatCurrency(price) }}</div> </template> <script> export default { data() { return { price: 100 } } } </script>
In general, the removal of filters in Vue 3 simplifies the API and encourages the use of methods and computed properties, which are just as powerful and flexible.
Vue 3.0 Built-in Filters:
Vue 3.0 has a set of built-in filters for common tasks like formatting dates, currency, and more. For instance, uppercase
, lowercase
, capitalize
, currency
, and date
are some examples.
<!-- Example --> <p>{{ message | uppercase }}</p>
Creating Custom Filters:
You can create custom filters using app.directive
in the setup function.
// Example custom filter to reverse a string app.directive('reverse', { mounted(el, binding) { el.textContent = binding.value.split('').reverse().join(''); }, });
<!-- Using the custom filter --> <p v-reverse>{{ message }}</p>
Applying Filters in Vue 3.0 Templates:
Filters can be applied directly in templates using the |
syntax.
<!-- Applying a built-in filter --> <p>{{ dateValue | date('YYYY-MM-DD') }}</p>
Vue 3.0 Filter Arguments: Filters can accept arguments for customization.
<!-- Using a filter with arguments --> <p>{{ price | currency('USD', 2) }}</p>
Chaining Filters: Filters can be chained to perform multiple transformations.
<!-- Chaining filters --> <p>{{ message | uppercase | reverse }}</p>
Vue 3.0 Filter Usage in Computed Properties: Filters can be used in computed properties for dynamic transformations.
computed: { formattedMessage() { return this.message | uppercase | reverse; }, },
Vue 3.0 Dynamic Filters: You can dynamically apply filters based on conditions.
<!-- Dynamic filter --> <p v-if="isUppercase">{{ message | uppercase }}</p> <p v-else>{{ message | lowercase }}</p>
Vue 3.0 Global Filters: Global filters can be defined and used across components.
app.config.globalProperties.$filters = { reverse(value) { return value.split('').reverse().join(''); }, };
<!-- Using a global filter --> <p>{{ message | $filters.reverse }}</p>
Using Filters with v-bind:
Filters can be used with v-bind
for dynamic attribute values.
<img :src="imagePath | addBaseUrl" alt="Product Image">
Updating Filters with Reactivity: Ensure reactivity by using reactive properties within filters.
computed: { reversedMessage() { return this.message.split('').reverse().join(''); }, },
<p>{{ reversedMessage }}</p>