⚠️ This lesson is retired and might contain outdated information.

Simplify Vue Components with vue-class-component

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 3 months ago

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.

Instructor: [00:00] 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.

[00:20] 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.

[00:35] 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.

[00:54] 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.

[01:19] 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.

[01:40] 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.

[01:56] 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.

[02:12] 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.

[02:32] 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."

[02:55] 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.

[03:21] 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.

Insart
Insart
~ 6 years ago

What the reason to use it? This is not trivial approach and it's difficult to explain to new people.

Erkan Buelbuel
Erkan Buelbuel
~ 6 years ago

is the following an error?

new Vue({
  el: "#root",   // <---- in Video is #app here (code) #root
  render: h => <App />
})

I think that is a poi-configuration? How can I extract poi-configration at all?

Erkan Buelbuel
Erkan Buelbuel
~ 6 years ago

Dear John in each lesson you use an another UI-Library, Tailwind, Tachyons, Semantic-UI,.. Meanwhile, I cannot memorize all the abbreviations, how do you do that? Dou you have a certain formula? Thank you for this great Video-tutorial, I never thought, I can see them all without falling asleep.

George Katsanos
George Katsanos
~ 6 years ago

I also will agree that this method of writing components looks (and is) unnecessary complex and has no real practical benefits.

Jack
Jack
~ 6 years ago

meh, so pointless.

Sergei
Sergei
~ 6 years ago

Hi John, I'm wondering how to use Vuex and mapGetters and mapActions with vue-class-component. Could you please point me in a right direction?

Wlad
Wlad
~ 6 years ago

Evan You (9/30/18 https://medium.com/the-vue-point/plans-for-the-next-iteration-of-vue-js-777ffea6fabf):

3.0 will support class-based components natively...

Fabrzio  Anichini
Fabrzio Anichini
~ 5 years ago

Cool, How we should structure the hook that come with the component lifecycle ( for example mounted, created...) in a class-based component?

Markdown supported.
Become a member to join the discussionEnroll Today