Filters

Formatting text is something that we need to do often while developing applications, whether that is to render monetary amounts, dates, or to transform/translate text.

Vue filters allow us to do this easily in a reusable manner. We can use Vue filters by using the pipe symbol: |. This is indeed inspired by Angular (this is getting old already), which itself has taken inspiration from classic Unix shells for its pipes.

Here's an example:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Vue filter</title> 
 
    <!-- development version of Vue.js --> 
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 
</head> 
<body> 
 
<div id="app"> 
    {{ message | reverse }} 
</div> 
 
<script type="text/javascript"> 
    let app = new Vue({ 
        el: '#app', 
        data: { 
            message: "Hello world!" 
        }, 
        filters: { 
            reverse: (value) => 
                value.split('').reduce((reversedString, char) 
=> char +
reversedString, '') } }); </script> </body> </html>

There's not much more to say here; if you understand Angular pipes, then Vue filters are quite intuitive.

Next up are lifecycle hooks.