Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create Filters in Vue.js


    Just like in the command line, you can pipe a property through a filter to get a desired result. You can even chain them together!



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Here's a list of dinosaurs. Notice that some start with an uppercase letter and others don't. We can use Vue.js filters to fix this issue. Let's start out by adding more to our template. We'll add a span here that shows the dinosaur's name and his weight. Then we'll add a Wikipedia link to the bottom.

    At this point we're in a world of trouble. Let's start cleaning this up by capitalizing all of our titles. We need to create a block called "filters" in our component. Let's create a function called "capitalize" that accepts a param called "value."

    After verifying that value is not empty and it is indeed a string, we'll return a version of it that begins with a capital letter. Now in Vue.js, filters only work inside the handlebar syntax of a template. We'll invoke the filter by piping it to capitalize. We'll do the same thing in our h4.

    Now that our titles are fixed, let's move on to the spans. We want them to be under cased. Let's create a new filter function called "under case." We'll pass a value into it. We'll do the same empty and string checking as before, and return a lower case version of the string.

    In our span, let's go ahead and pipe to under case. We'll do the same thing in our anchor. One of the powerful things about filters in Vue.js is you can chain them together. Let's create URL filter. That filter's also going to take a value. After our glorious string empty combo, we're going to return a wiki link.

    Let's chain these guys together. We're going to remove the old link. After under case, we're just going to pipe it to URL. That way dino.text moves through both the under case and URL filters.