
Published on March 15, 2023 by Eunbi.N
Vue Vue3 Migration Composition API
# Vue 2 버전 확인
npm list vue
# 사용 중인 Vue 관련 패키지 확인
npm list | grep vue
# Vue 3 및 관련 패키지 설치
npm install vue@next
npm install vue-router@4
npm install vuex@4
# 또는 Pinia 사용 (권장)
npm install pinia
# 개발 의존성 업데이트
npm install --save-dev @vue/compiler-sfc
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
const app = createApp(App);
app.use(router);
app.use(store);
app.mount("#app");
const app = createApp(App);
app.component("MyComponent", MyComponent);
// router/index.js (Vue 2)
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/Home.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes,
});
export default router;
// stores/counter.js
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", {
state: () => ({
count: 0,
}),
getters: {
doubleCount: (state) => state.count * 2,
},
actions: {
increment() {
this.count++;
},
},
});
<template>
<div>
<h1></h1>
<p>Count: 8</p>
<p>Double: </p>
<button @click="increment">+</button>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
const title = ref('Counter App')
const count = ref(0)
const doubleCount = computed(() => count.value * 2)
const increment = () => {
count.value++
}
onMounted(() => {
console.log('Component mounted')
})
</script>
// vite.config.js
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
vue: "@vue/compat",
},
},
});
# 마이그레이션 도우미 설치
npm install -g @vue/cli-service
# 마이그레이션 체크
vue-cli-service migrate
# Vue 3 ESLint 플러그인 설치
npm install --save-dev eslint-plugin-vue@next
# .eslintrc.js 설정
module.exports = {
extends: [
'plugin:vue/vue3-essential'
]
}