๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/Vue.js

[Vue.js] vue-router ์„ค์น˜ ํ›„ ๋ผ์šฐํŒ…ํ•˜๊ธฐ

by Connecting-the-dots 2022. 4. 5.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!


๐Ÿ’œ vue-router ์„ค์น˜ ๋ฐ ์…‹ํŒ…ํ•˜๊ธฐ

  • ํŽ˜์ด์ง€๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ Router ๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • Vue ์—์„œ๋Š” vue-router ๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋ฉด Router ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿค vue-router 4 ๋ฒ„์ „ ์„ค์น˜ํ•˜๊ธฐ

npm install vue-router@4
  • ์œ„์™€ ๊ฐ™์ด ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•ด์„œ vue-router 4 ๋ฒ„์ „์„ ์„ค์น˜ํ•œ๋‹ค.
  • yarn ์ด ์„ค์น˜๋˜์–ด์žˆ๋‹ค๋ฉด npm install ๋Œ€์‹ ์— yarn add ๋ฅผ ์ด์šฉํ•ด๋„ ๋œ๋‹ค.
  • vue-router ๋ฅผ ์„ค์น˜ํ•˜๊ธฐ ์ „์— ๋งŒ์•ฝ npm run serve ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ง„ํ–‰์ค‘์ด๋ผ๋ฉด ์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•œ ๋‹ค์Œ์— ์ง„ํ–‰ํ•ด์ฃผ์–ด์•ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿค router.js ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

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

const routes = [
  {
    path: "/๊ฒฝ๋กœ",
    component: importํ•ด์˜จ ์ปดํฌ๋„ŒํŠธ,
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;
  • vue-router ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด src ํด๋” ์•ˆ์— router.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • path ์™€ component ๋งŒ ์ˆ˜์ • ํ˜น์€ ์ถ”๊ฐ€ํ•ด๊ฐ€๋ฉฐ ํŽ˜์ด์ง€๋“ค์„ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿค main.js ํŒŒ์ผ์— router.js ํŒŒ์ผ import ํ•ด์ฃผ๊ธฐ

import router from './router.js'
createApp(App).use(router).mount('#app')
  • main.js ํŒŒ์ผ์— router.js ํŒŒ์ผ์„ router ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ import ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  createApp(App).mount('#app') ์ด๋ผ๋Š” ์ฝ”๋“œ ์ค‘๊ฐ„์— .use(router) ๋ผ๋Š” ์ฝ”๋“œ๋„ ์‚ฝ์ž…ํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿค App.vue ํŒŒ์ผ์— <router-view></router-view> ์ถ”๊ฐ€ํ•˜๊ธฐ

  • App.vue ํŒŒ์ผ์— ์žˆ๋Š” <template></template> ๋‚ด์˜ ์›ํ•˜๋Š” ์œ„์น˜์— <router-view></router-view> ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด Router ๋กœ ๊ตฌ๋ถ„๋œ ํŽ˜์ด์ง€๋ฅผ ํ•ด๋‹น ์œ„์น˜์— ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
  • ๋งŒ์•ฝ ์ „์†กํ•ด์•ผํ•  props ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ธฐ์กด์— Component ์‚ฌ์šฉํ•  ๋•Œ์˜ props ๋ฅผ ์ „์†กํ•œ ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ <router-view> ์— ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿค router.js ํŒŒ์ผ์— Route ๋งŒ๋“ค๊ธฐ

import List from './components/List.vue';

const routes = [
  {
    path: '/list',
    component: List,
  }
];
  • ์ด์ œ router.js ํŒŒ์ผ์—์„œ ํŠน์ • ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ ํŠน์ • Component ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, /list ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ List.vue ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.
    • List.vue ํŒŒ์ผ์„ List ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ import ํ•œ๋‹ค.
    • path ๋ถ€๋ถ„์— '/list' ๋ผ๊ณ  ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    • component ๋ถ€๋ถ„์€ List.vue ๋ฅผ import ํ•  ๋•Œ ์ง€์–ด์ค€ ์ด๋ฆ„์ธ List ๋กœ ์ž‘์„ฑํ•œ๋‹ค.
const routes = [
  {
    path: "/",
    component: Main,
  },
  {
    path: "/list",
    component: List,
  }
];
  • ๋งŒ์•ฝ Route ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด object ์ž๋ฃŒํ˜•์œผ๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿค ํŽ˜์ด์ง€ ์ด๋™ ๋งํฌ ๋งŒ๋“ค๊ธฐ

<router-link to="/list">์ด๋™ํ•˜๊ธฐ</router-link>
  • <a></a> ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ํŽ˜์ด์ง€ ์ด๋™ ๋งํฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด, ์œ„์™€ ๊ฐ™์ด <router-link></router-link>๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • to="์ด๋™ํ• ๊ฒฝ๋กœ" ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์›ํ•˜๋Š” ๋Œ€๋กœ ๊ฒฝ๋กœ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•