๐Ÿ“† 23.03.08 - Vue 3์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ์šฉํ•˜์—ฌ ์ฐ์–ด๋จน์–ด๋ณด๊ธฐ๐Ÿคค

๋ฒ„๋“คยท2023๋…„ 3์›” 8์ผ
0

โœจToday I Learn (TIL)

๋ชฉ๋ก ๋ณด๊ธฐ
29/62
post-thumbnail
post-custom-banner

์ €๋ฒˆ ๊ธ€์—์„œ ๋‹ค๋ค˜๋˜ Vue 3์—์„œ์˜ ์—ฐ์žฅ์„ ์ž…๋‹ˆ๋‹ค๐Ÿฅฐ
https://velog.io/@buddle6091/23.02.10-Vue-3..-%EC%9D%B4%EC%A0%84-%EB%B2%84%EC%A0%84%EA%B3%BC%EB%8A%94-%EB%AC%B4%EC%97%87%EC%9D%B4-%EB%8B%AC%EB%9D%BC%EC%A1%8C%EC%9D%84%EA%B9%8C

Vue 3 + Typescript + Vite

Vue 3์— ๊ด€ํ•˜์—ฌ๋Š” ์•ž์„  ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃฌ ๋ถ€๋ถ„์ด ์žˆ์–ด ์˜ค๋Š˜์€ ํ•ด๋‹น ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์„ค์น˜ํ–ˆ์„ ์‹œ์˜ ํ™˜๊ฒฝ์ด ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ์ด ๋˜์–ด์žˆ๋Š”์ง€ ๋ณด๋ ค๊ณ  ํ•œ๋‹ค!


Vue 3๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ค์น˜ํ•ด ๋ณด๊ธฐ

npm init vue@latest ๋ช…๋ น์–ด๋กœ ํ†ตํ•ด ํ•ด๋‹น Vue ํ”„๋กœ์ ํŠธ๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ์ž‘์—…์„ ํ•  ๊ฒƒ์ธ์ง€ ์„ค์ •์„ ํ•˜๊ฒŒ๋œ๋‹ค.

โœ” Project name: โ€ฆ <your-project-name>
โœ” Add TypeScript? โ€ฆ No / Yes --> Yes for set typescript
โœ” Add JSX Support? โ€ฆ No / Yes
โœ” Add Vue Router for Single Page Application development? โ€ฆ No / Yes
โœ” Add Pinia for state management? โ€ฆ No / Yes
โœ” Add Vitest for Unit testing? โ€ฆ No / Yes
โœ” Add Cypress for both Unit and End-to-End testing? โ€ฆ No / Yes
โœ” Add ESLint for code quality? โ€ฆ No / Yes
โœ” Add Prettier for code formatting? โ€ฆ No / Yes

Scaffolding project in ./<your-project-name>...
Done.

์ด๋Ÿฌํ•œ ์ž‘์—…์„ ๊ฑฐ์น˜๋ฉด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ฐ˜์˜ Vue 3 ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐ ๊ตฌ์„ฑ์ด ์™„๋ฃŒ๊ฐ€ ๋œ๋‹ค.

Vite

์•ž์„  ํฌ์ŠคํŠธ์—์„œ ๋งํ–ˆ๋“ฏ์ด, Vue 3์˜ ๋ฒˆ๋“ค๋Ÿฌ๋กœ Webpack ๋Œ€์‹  vite ๋ฅผ Default๋กœ ์ œ๊ณตํ•œ๋‹ค.

webpack.config ํŒŒ์ผ์ด ์กด์žฌํ–ˆ๋“ฏ์ด, Vite ๋˜ํ•œ vite.config ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์–ด ์—ฌ๊ธฐ์„œ ๋ฒˆ๋“ค๋ง ๊ด€๋ จ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

// vite.config.ts
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ๋Š” Vue๋กœ ์„ค์ •๋˜์–ด์žˆ๊ธฐ์— defineConfig๋ผ๋Š” ์„ค์ • ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด์— plugin: [vue()] ๋กœ ์ ํ˜€์ ธ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  resolve.alias ํ†ตํ•ด์„œ๋Š” ./src ํด๋” ๋‚ด์—์„œ @ ์œผ๋กœ import ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ์ปดํฌ๋„ŒํŠธ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ„๋žตํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋‹น ์„ค์ •์œผ๋กœ ๊ฐ€๋Šฅ์ผ€ ํ•ด์ค€๋‹ค.
vscode์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ด€๋ จ ํ”Œ๋Ÿฌ๊ทธ์ธ vue 3๋Š” volar ๋ฅผ ์„ค์น˜ํ•˜๊ณ  ctrl + space ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ด€๋ จ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ์„ค์ •์„ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋” ๋นจ๋ฆฌ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

Router

๋ผ์šฐํ„ฐ๋Š” ์ด์ „ ๋ฒ„์ „๊ณผ ๊ฐ™์ด npm install vue-router ๋กœ ์„ค์น˜๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ผ์šฐํ„ฐ ํด๋”๊ตฌ์กฐ

์ถ”ํ›„์˜ ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด๋ฅผ ์œ„ํ•ด ํ•„์ž์˜ ๊ฒฝ์šฐ src/router/index.ts ์˜ ๋ฃจํŠธ๋กœ ๋ผ์šฐํ„ฐ ํด๋”๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜์˜€๋‹ค.

๊ทธ ๋‹ค์Œ index.ts ์—์„œ ๋ผ์šฐํ„ฐ ํŽ˜์ด์ง€ ๋ณ„๋กœ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

import { createRouter, createWebHistory } from "vue-router";

const routes: Array<any> = [
  {
    path: "/",
    component: () => import("../pages/Home.vue"),
  },
  {
    path: "/2",
    component: () => import("../pages/Page2.vue"),
  },
  {
    path: "/3",
    component: () => import("../pages/Page3.vue"),
  },
];

export default createRouter({
  history: createWebHistory(),
  routes,
});

์—ฌ๊ธฐ์„œ ์›๋ž˜ RouteRecordRaw ๋ผ๋Š” ํƒ€์ž…์ด any ๋Œ€์‹  ๋“ค์–ด๊ฐ€์•ผํ•˜์ง€๋งŒ, ๊ณ„์†ํ•ด์„œ ์ธ์‹ ์˜ค๋ฅ˜๊ฐ€ ์ผ์–ด๋‚˜์„œ ์ผ๋‹จ Any๋กœ ๋Œ€์ฒดํ–ˆ๋‹ค.. ์กฐ๋งŒ๊ฐ„ ํ•ด๊ฒฐ์ ์„ ์ฐพ์•„๋ณผ ์˜ˆ์ •์ด๋‹ค.

main.ts ํŒŒ์ผ์— router ์ถ”๊ฐ€

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router/index";

import "./assets/main.css";

createApp(App).mount("#app");

App.vue ์ „์—ญ์„ค์ •

๊ทธ๋ฆฌ๊ณ  App.vue ์—์„œ๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ์ „์—ญ์œผ๋กœ ๋ฟŒ๋ ค์ค˜์•ผ๋˜๊ธฐ์— ์„ค์ •์„ ํ•ด์ค€๋‹ค.

<template>
  <Header />
  <router-view></router-view>
  <SideBar />
</template>
profile
ํƒœ์–ด๋‚œ ๊น€์— ๋งŽ์€ ๊ฒฝํ—˜์„ ํ•˜๋ ค๊ณ  ์•„๋“ฑ๋ฐ”๋“ฑ ์• ์“ฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€