Become a member
to unlock all features

Level Up!

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


    Simplify Vue Components with vue-class-component

    John LindquistJohn Lindquist

    While traditional Vue components require a data function which returns an object and a method object with your handlers, vue-class-component flattens component development by allowing you to add your data properties and handlers directly as properties of your class.



    Become a Member to view code

    You must be a 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




    Instructor: Create a new file. We'll call this app.vue. Set up a basic template with a div and a message in it. Then we'll add our script tag. Instead of the usual exportDefault object, we're going to export a class of app, which extends Vue.

    We'll need to import vue from Vue, then instead of a data function which returns an object for our data, we can just say message is, "Hello from class." Now, to make this work, I do need to npm install vue-class-component.

    Then with vue-class-component installed, I can import component from vue-class-component, and decorate my class with the component function, where just object we pass in are some options. You'll notice right away that VS Code complains about experimental decorators.

    Your editor may do the same, so I'm going to remove that warning with a JS config.json file, and simply tell the compiler that we want to allow experimental decorators. Then back in our app.vue file, if we reload the window, you'll see that error goes away, because it's picked up our configuration to allow decorators.

    To use this, we just import app from app. Now, tell Vue to render our app. You'll see the result is, "Hello from class." Now, I am using Poi in my terminal to run and build this project. Poi comes preconfigured with the Babel plugin that allows decorators.

    If you look at the list of defaults in Poi, you can see we get decorators, class properties, which is this, and other standard features you would expect to use in a Vue project. If you don't want to install the Babel plugin for decorators, you can always just ditch that at sign.

    A decorator is really just a functional. I'll take this closing paren, paste it there, and take exportDefault, and paste it up here. Yeah, the decorator is a function which returns a function, then invokes it with the thing underneath.

    This, if I hit save, is the exact same thing as if I were to do that. Now, my personal reasoning for using vue-class-component, rather than writing a, we'll just call this, traditional is that Vue deals a lot with the this concept in JavaScript.

    When you write a data function, you have to return an object like that. You'll use methods, which is an object, like an onClick, which would change. If I had a message of "hello" and then I'd access this.message is "goodbye."

    The equivalent in vue-class-component is simply saying on click, this.message equals goodbye. We'll add the onClick here. Click is onClick. I'll go ahead and click this. It changes to goodbye, and you can see if I grab this export default, cut that, paste it here, we'll just remove all of this. We're just exporting the object.

    You can see we have this same thing, hello. I click, and it changes to goodbye. Because Vue works so much with this, I find it much easier to read in class. As a general rule, when I use this a lot, I stick to classes, versus an object, where you're invoking a data function, returning an object, and methods with functions accessing this. I find vue-class-component much easier to manage.