As we know, the official release of Vue 3 was on September 18, 2020, it’s been almost a year, and I think it’s time to move on to it. Buuuut… it’s not that simple.
At the moment, not all plugins are adapted to Vue 3, so you have to look for replacements. I tried it and you know, it didn’t work.
But to our great luck, there is a plugin that has the same API as Vue 3. It was kindly provided by the developers themselves to soften the transition from version 2 to version 3. The plugin is called @vue/composition-api. Today we will talk about it.
What to expect from this article
Important: This article is an introductory one with which I plan to start a series of articles on VUE 3. So, today I will show you just a few simple examples that will help whet your appetite and smoothly start your transition to the new version of Vue.
Let’s get started!
First, we need to update the packages. To do this, open a terminal and use the command:
But note that you must have Vue CLI and Node.js installed.
After running the command, you should see a list of packages that need to be updated. If the packages are updated, the message below will appear
DONE Seems all plugins are up to date. Good work!
Install the Vue Composition api plugin
yarn add @vue/composition-api// ornpm i @vue/composition-api
After that, create a file in the scr folder where @vue/composition-api will be initialized. Let’s call it installCompositionApi.js and add the code
import Vue from 'vue' import VueCompositionApi from '@vue/composition-api'Vue.use(VueCompositionApi)
This file must be imported into the main.js file, and the import function must be placed first in order for the scripts to initialize properly.
This concludes the installation of @vue/composition-api. The library is available globally and can be used in any of the files.
Rewriting the Vuex store
The next step is to start rewriting the existing code. I would start with the Vuex store. Let’s do that. Let’s take one of the files, for example, the module responsible for the list of articles.
You have to agree it’s pretty wordy. We have to write an additional layer in the form of mutations, actions to write data asynchronously. But come on.
We delete everything without remorse and add this code:
The number of lines is reduced, and that is already good. Let’s sort it out.
The first line imports the method ref. It adds reactivity for any variable.
ref takes an argument and returns it wrapped in an object with the property value, which can then be used to access or change the value of the reactive variable.
In the code above, ref has been added for the articles variable, it is now reactive and has additional properties.
Then we see a function that gets all articles by API and writes them into the articles variable.
But please note that it writes them to the reactive value property. If it doesn’t, the value won’t change.
That’s all. Our updated store works in exactly the same way and is even much simpler.
The only question that remains is whether Vuex is needed now?
Rewriting the component
After updating the code in our store, let’s rewrite the component that is responsible for displaying the articles.
The component so far looks like this:
Here we see the good old Vue 2 syntax. Let’s rewrite it to the new one, and we should get it like this:
Let me tell you right away: in @vue/composition-api and therefore in Vue 3, “this” is no longer available. You must use context instead. I’ll talk about it a bit later.
Let’s take apart the new rewritten component.
At the beginning of the <script> block the hooks are imported. Yes, now you have to import them to use them.
Below, note the setup method, it is the entry point where all the magic of reactivity happens. It has two arguments:
- props: through it we get props, which are passed to the component
- context: this is exactly what replaces “this”. Through it, you can access the emit method, the slot variable, attrs, etc. Here is the complete list.
The global “this” object is now unavailable in components and we cannot use it to access, for example, the $route object or any other global object. To solve this, we added the property root. Now it’s the only way to access all global objects, installed plugins, etc.
Note, in the rewritten component, in the computed hook, we return the articles variable with the value property. This must be done, otherwise, the articlesList variable will have a reactive object, something like this:
In Vue 3, all reactive variables now have a value property.
But when using reactive variables in a template, you don’t need to call the value property as here:
The object returned by the setup() function will be processed, and the value properties will be discarded.
And in order to have access to the properties from setup in the template block, you have to return them with the return method.
At this point, I would like to complete the discussion. A more detailed discussion of the topic will follow in the future articles in our blog.
The process of upgrading to version 3 using the @vue/composition-api plugin was very easy because it is fully compatible with Vue 2. The syntax is uncomplicated and easy to understand. I hope you won’t have any trouble mastering it.
At this point I want to end and continue a more detailed discussion of the topics in future articles.
To learn more about the Vue Composition API, follow this link.
Thank you and see you soon!
Strapi: Things to Keep in Mind Before Migrating to...
Strapi: Things to Keep in Mind Before...
Strapi: Things to Keep in Mind Before Migrating to This CMS
When it comes to CMS, the modern market has already stepped so much that the choice is becoming increasingly difficult. There are so many options for...