8

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.

Farsen
  • 1,387
  • 3
  • 21
  • 48

4 Answers4

15

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

JBS
  • 993
  • 5
  • 11
  • 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 Jun 27 '22 at 07: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: https://www.larssommer.dk/ExampleComp.vue - https://www.larssommer.dk/ExampleCompShort.vue – Farsen Jun 27 '22 at 14:02
  • I rewrote the code with some explanations and you can find it here: https://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 Jun 27 '22 at 20:59
  • Thanks alot Jakob! I think it´s easier to make a decision on what to do from here, now :) – Farsen Jun 28 '22 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 May 09 '23 at 01: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 vue@2.7. 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 vue@2.7 (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 .

Kevin Boosten
  • 244
  • 1
  • 11
  • 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 Dec 21 '22 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.) – Kevin Boosten Dec 23 '22 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.

0

Updated answer:

To transform your Vue property decorator to setup API, follow the steps outlined below:

First, globally install the vue-declassify-to-setup package by running the following command in your terminal:

npm install -g vue-declassify-to-setup

Then, to understand the usage and options:

vue-convert --help

More info

https://www.npmjs.com/package/vue-declassify-to-setup

Old answer:

I've just shared a conversion script on GitHub that allows for batch conversion of a folder of *.vue files, specifically transforming vue-property-decorator to "script setup".

You can find the script at this repository: https://github.com/otv1/vue-property-decorator-to-vue-3-setup-api.

While creating this script, I wasn't aware of existing alternatives. Nonetheless, here you have one more options. Its not perfect, but may still save you a lot of manual work.

  • Interesting, I will take a look at it when we begin the process in a couple of months. Thanks for your contribution – Farsen May 29 '23 at 19:58
  • I gave the script a try, but unfortunately it does not seem to work with my particular project. I use single quotes, and it doesent convert @Prop´s and does not insert function keyword before vue2 methods. I´m sure it will help other people though :) – Farsen Jun 27 '23 at 19:44