11

I maintain quite a large vue2 project, which among other libraries makes use of Vuetify, vue-class-component, vue-property-decorator and typescript. Example component:

(The component is simplified and not compilable for the sake of displaying the syntax) (The component is simplified and not compilable for the sake of displaying the syntax)

We are very satisfied with this setup, because of the simple syntax, it´s so easy for new and existing developers to understand, and because we haven´t had any real problems with this setup in it´s current lifespan of 2-3 years. The official support for vue2 is coming to and end, and we would like to upgrade to vue3. Our problem is now which path we should follow, and what is possible. I´ve been researching for quite some time now, but are still confused on the topic. The Composition API in vue3 has been made for reason, and people seem to be generally happy with it, which I have a hard time understanding. Besides a couple of cool new features, The Composition API seems like a downgrade to me in terms of not-easy-to-understand, "redundant", and a too explicit syntax. A couple of examples:

Props:

Vue2
@Prop({ default: '' }
label!: MyProp;

Vue3
props: {
  myProp: {
    type: Object as PropType<MyProp>, //Kind of weird explicit type definition 
    default: ''
  },
}

Variables (same thing goes for functions)

Vue2
myValue: string = ""; //Reactive and available in the template

Vue3
setup(props, context) { //Must be setup in the setup function to make it available in the template
  const myValue: Ref<string> = ref(""); //Must declare it reactive with this funky explicit syntax
  return {
    myDataValue, //Must be returned from setup function
  };
}

Computed properties

Vue2 
get myValue(): string {
    return this.myDataValue.toLowerCase();
}

Vue3
setup(props, context) {
  const myValue: ComputedRef<string> = computed(() => {
    return myDataValue.value.toLowerCase(); //Accessing a value requires .value
  });
}

Even if we decided to go with the Composition API, it would be very expensive, time-wise, to rewrite our code-base.

I´ve tried creating a sample vue3 class-style component project from scratch using the Options API (following this guide), and afterwards copy some of my existing components into the project. This seems to work after som fiddling around not using too much time. So when Vuetify 3 finally release and we can migrate, my questions are:

  1. Is Vue3 Options API + vue-property-decorator a viable way to go?
  2. The vue-property-decorator library has not been updated for two years! And there is no official vue3 support documentation. So is this vue-property-decorator with vue3 just a very bad idea?
  3. Is it really worth the time and effort migrating to the Composition API?

Thanks!

Update 08/01/2023:

We tried a few tests Vue3 Composition API before the migration. The syntax and the "troublesome" way of doing things made us go with vue-facing-decorator instead. This is basically the same as the vue-property-decorator, and we are so far very happy with our choice 👍 Thanks alot for the contributions.

1
  • what about Decorators? Do they work?
    – canbax
    Commented Jul 19, 2024 at 12:34

4 Answers 4

18

We are using Vue 2 with a lot of components built on top of class based components using vue-class-component and vue-property-decorator.

Now we need to upgrade to Vue 3 for better performance and long time support.

To answer your questions:

  1. I did a lot of research and there is no plan to support class based components going forward from here. The two plugins from above are stuck at RC level for Vue 3. See: https://github.com/vuejs/vue-class-component/issues/406

  2. A library which is not maintained and/or properly tested with a new Version of Vue is never a good idea to use imo.

  3. Thats a question where there is no clear answer for.

You can try this strategy, maybe it works for you too:

We added the composition API and script setup via these plugins to our existing Vue 2 code base:

Update 22/07/10: Vue 2.7 (the last minor release) was released, there is no need for the plugins above when using this version: https://blog.vuejs.org/posts/vue-2-7-naruto.html

This allows us to use composition API + script setup and class based components simultaneously. So we are beginning to write new components with the new syntax and rewriting old code step by step. When we are done with this process, we are going to migrate to Vue 3 using the migration guide:

That is a lot of work and very annoying to do, as it takes a lot of time away from new features or bugfixes which are more important than dealing with this upgrade pain. But we hope going forward from here that Vue is going to me more stable in their decisions and how they want to continue forward.

I also agree with you saying that class based components are a more elegant way to do things than the composition API. It is also shorter and closer to backend programming languages. Its very sad to see this one go.

All the best Jakob

Update 23/04/14: Great new article on how one could continue using class based components: https://medium.com/@robert.helms1/vue-2-to-vue-3-with-class-components-cdd6530a2b2a

Further ressources:

Guide: https://levelup.gitconnected.com/from-vue-class-component-to-composition-api-ef3c3dd5fdda

More reasons why they drop it:

https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121

https://github.com/vuejs/rfcs/blob/function-apis/active-rfcs/0000-function-api.md#type-issues-with-class-api

https://vuejs.org/guide/extras/composition-api-faq.html#better-type-inference

5
  • Thanks alot for your adequate answer! Alright, so I guess the "good old" class syntax unfortunately is not the way to go. And it saddens me to hear that you use the words 'annoying' and 'pain' describing the migration process. Especially when it does not seem like an upgrade. But it´s still kind of blurry to me, how your syntax ends up looking like. Can you please share how an example component looks like? Thanks! Seems like we might as well could rewrite the application in Svelte which has an even more simpler syntax than vue2. But that would of course come with a price too ;)
    – Farsen
    Commented Jun 27, 2022 at 7:25
  • Ok, guess I overinterpreted the meaning ;) That´s very kind of you! Here is links to the full version of the file, and a short one that has been cut down: larssommer.dk/ExampleComp.vue - larssommer.dk/ExampleCompShort.vue
    – Farsen
    Commented Jun 27, 2022 at 14:02
  • I rewrote the code with some explanations and you can find it here: gist.github.com/jschweighofer/65e20528d3d0fa3afffd1455c95599f0 Please be aware that this is our solution and must not fit for you or your company. Please check for yourself, take it as some inspiration. :)
    – JBS
    Commented Jun 27, 2022 at 20:59
  • Thanks alot Jakob! I think it´s easier to make a decision on what to do from here, now :)
    – Farsen
    Commented Jun 28, 2022 at 11:22
  • 1
    vue-facing-decorator has worked well for us! It’s really saved us a ton of time migrating our class components to Vue 3
    – rdhelms
    Commented May 9, 2023 at 1:37
4

FWIW, despite the fact that I am really more a fan of using class based components, we made the decision to convert all our ±300 components to Options API. So at least we can upgrade to Vue@3 and Vite. Changing the complete codebase to Composition API (with <script setup>) is not worth it and potentially error prone. All new components will be written with <script setup> though.

Steps that we followed to get there:

  1. Make sure you are at [email protected]. This version includes backported features from vue@3, including Composition API and <script setup>. See their blog.
  2. Check and update all dependencies to be compatible with [email protected] (e.g. we make extensive use of AgGrid that needed to be at version 27).
  3. Gradually convert all class components to Options API via this package: https://www.npmjs.com/package/vue-declassify. (Only the @Emit decorator needs to be handled manually). cd into a directory that you want to convert and run this command: find . -name "*.vue" -exec vue-declassify {} \; to execute the converted on multiple vue files sequentially.

This process took us approximately 2 days to execute completely.

NB: Make sure that your codebase is thoroughly backed by unit and e2e tests prior starting this process 🙂.

2
  • 1
    Thanks alot, I will definetly look into this package when we are about migrate to vue3. But did you prevoiusly use vue-property-decorator? If yes, are you satisfied with the new solution?
    – Farsen
    Commented Dec 21, 2022 at 12:41
  • Yes, we used vue-class-component with vue-property-decorator. Like I said, I am more a fan of class based components to be honest 😏. I think that is related to the specific syntax introduced by vue in the composition api (e.g. ref, computed etc.) Commented Dec 23, 2022 at 22:43
1

We have exactly the same problem with vue-property-decorator.

You can temporarily use these candidate release versions, the only change that's needed is to replace @Component decorator to @Options and it seemed to work for me when I was migrating the project from vue2 -> vue3

"vue-class-component": "^8.0.0-rc.1"
"vue-property-decorator": "10.0.0-rc.3"

Nevertheless both of the libraries seem dead and I don't think they will be officially supported, so you probably need to switch to Composition / Options API anyways.

1
  • 2
    both packages now suggest vue-facing-decorator in their deprecation notices
    – rdhelms
    Commented May 9, 2023 at 1:38
1

Use this, it was created specifically for Vue3

https://facing-dev.github.io/vue-facing-decorator/#/?id=information

3
  • Welcome to Stack Overflow! Your answer could be improved with additional supporting information. If possible, please edit your post to add further details, such as explanations sample code. You can find more information in How do I write a good answer? - "Brevity is acceptable, but fuller explanations are better." It might be helpful to review some highly upvoted questions. Thanks! Commented Mar 19, 2024 at 17:24
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 20, 2024 at 8:59
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Mar 22, 2024 at 9:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.