Professional Documents
Culture Documents
How To Migrate From Vue 2.0 To Vue 3.0 Composition API With TS (Part 2 - Replace)
How To Migrate From Vue 2.0 To Vue 3.0 Composition API With TS (Part 2 - Replace)
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 1/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
In October 2019, Vue 3.0 pre-alpha was finally released. So it’s the matter of
time to use Vue 3.0 as production.
To prepare for it, I would like to share how to migrate from Vue 2.0 to Vue
3.0. Because these days large amount of Vue users has already introduced
TypeScript, this article also use TypeScript.
Because the topic is bit long, I splitted it into 3 articles. This article is the
continuation of the first step. If you want to start from the specific part, feel
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 2/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
// src/List.vue
<script lang="ts">
import { createComponent } from "@vue/composition-api";
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 4/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
But Don’t worry. This error is from Vetur. Everything works fine.
// src/List.vue
<script lang="ts">
import { FilterByEnum, Data, FilterBy } from "./types/List";
import { createComponent, reactive } from "@vue/composition-api";
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 5/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
Actually, you have different API to make variables reactive called ref() .
But the function itself is pretty similar to reactive() . Please check the
// src/List.vue
...
import todos from "../utils/todos";
}
})
Because you don’t have to use this to access local state, it’s simpler.
So far, you have the function to initialize todos. But when do you call it ?
Yes, you want to do it when Vue instance is “Mounted”.
Of course Vue 3.0 has lifecycle hooks. You should use it like below.
// src/List.vue
...
}, 1000);
}
})
You can move rest of methods to inside setup function. After moving all
method, List.vue becomes like below.
// src/List.vue
<script lang="ts">
import Vue from "vue";
import todos from "../utils/todos";
import { FilterByEnum, Data, FilterBy } from "./types/List";
import { createComponent, reactive, onMounted } from
"@vue/composition-api";
onMounted(() => {
init();
});
One thing you don’t know yet is, two arguments of setup, props and context.
Props is easy to understand. You can get a props inside of setup function. In
the context, you can check many things which is seen in this of Vue 2.0 like
slot , parent and root .
// src/List.vue
...
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 10/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
...
State
- todos
- newTodo
- filterBy
Computed
- filteredTodos
- numOfTodos
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 11/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
methods
- addTodo
- deleteTodo
- completeTodo
- handleClickFilterBy
- goEditTodo
But if you want to use them inside template, what you should do ? It’s really
easy. Just return what you need inside setup function.
// src/List.vue
...
handleClickFilterBy,
completeTodo,
goEditTodo,
deleteTodo
};
}
})
One thing you don’t know is toRefs . It’s because when you use reactive
without reRefs , you must use them inside temple like todoState.todos ,
todoState.filterBy . But it’s not DRY ! With toRefs() and destructuring,
you can use them same as before.
Make it composable
So far, Vue 3.0 is perfectly works. But you can refactor it. The big purpose of
Vue 3.0 composition API is to make codes composable. It means that code
should be separated in small parts and make them use again and again.
// src/List.vue
...
// src/List.vue
...
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 14/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
...
return {
...toRefs(todoState),
filteredTodos,
numOfTodos,
addTodo,
handleClickFilterBy,
completeTodo,
deleteTodo
}
}
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 15/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
// src/List.vue
...
return {
...useTodos(),
goEditTodo
};
}
})
return {
...toRefs(todoState),
filteredTodos,
numOfTodos,
addTodo,
handleClickFilterBy,
completeTodo,
This is your last free story this month. Sign up and get an extra one for free.
deleteTodo
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 16/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
}
}
Because composition function is just a function, you can easily separate it.
From Linus Borg’s demo repo, those composition functions are saved to
src/composables/ . And each composition functions are named like useHoge
like below. Actually, the type file is also moved to under composables.
// composables/useTodos.ts
import todos from "../utils/todos";
import { FilterByEnum, Data, FilterBy } from "./types/UseTodos";
import { reactive, onMounted, computed, toRefs } from
"@vue/composition-api";
// src/List.vue
<script lang="ts">
import Vue from "vue";
import { createComponent } from "@vue/composition-api";
import { useTodos } from "../composables/useTodos";
Now, List.vue is super clean. With Compostion API, you can separate logics
from vue component and vue component can focus more on View. That’s
the one big value of Vue 3.0.
Next Level
So far, the composition function is called in List.vue component. What if you
call same composition function in Edit.vue ? Are the states and functions
shared between components ??
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 18/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
But if you use Provide and Inject of Vue, you can create global store. So next
article explains how to create global store with Vue 3.0.
. . .
In this article, I explained how to replace Vue 2.0 to Vue 3.0. But this article
doesn’t talk about global store of Vue yet. I would like to share the
possiblity of Vue 3.0 global store and try to make it.
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 19/20
4/1/2020 How to migrate from Vue 2.0 to Vue 3.0 Composition API with TS (Part 2: Replace)
This is your last free story this month. Sign up and get an extra one for free.
https://itnext.io/how-to-migrate-from-vue-2-0-to-vue-3-0-composition-api-with-ts-part-2-replace-73606fb1b296 20/20