How to migrate from Vue 2 to Vue 3?

Migration to Vue 3

How to migrate from Vue 2 to Vue 3?

Introduction

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!

Installing @vue/composition-api

First, we need to update the packages. To do this, open a terminal and use the command:

vue upgrade

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