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

[Vue.js] Nested routes ์™€ push ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

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

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


๐Ÿ’œ nested routes ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

  • /detail/0/author ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ•˜๋ฉด, detail ํŽ˜์ด์ง€ ๋‚ด์— ์ž‘์„ฑ์ž๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , /detail/0/comment ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ•˜๋ฉด detail ํŽ˜์ด์ง€ ๋‚ด์— ๋Œ“๊ธ€์„ ๋ณด์—ฌ์ฃผ๋Š” ๋“ฑ route ๋กœ ๋‚˜๋ˆˆ ํŽ˜์ด์ง€ ์•ˆ์— ๋˜๋‹ค์‹œ route ๋กœ ๋‚˜๋ˆˆ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ํŠน์ • ํŽ˜์ด์ง€ ๋‚ด์— ๋˜ route ๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์„ nested routes ๋ผ๊ณ  ํ•œ๋‹ค.
  • ๋ฌผ๋ก  ๋ชจ๋‹ฌ์ฐฝ์ฒ˜๋Ÿผ UI ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ํ™œ์šฉํ•ด๋„ ์œ ์‚ฌํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ธด ํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ URL ์„ ๋‚˜๋ˆ ๋‘๋ฉด ์•ž์œผ๋กœ๊ฐ€๊ธฐ ํ˜น์€ ๋’ค๋กœ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์ด ๋™์ž‘ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ, ํ•„์š”์— ๋”ฐ๋ผ nested routes ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿค nested routes ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

// ์ „์ฒด ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
// ์ฝ”๋“œ ๋‚ด์šฉ์ด ๊ธธ์–ด ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์€ ์ œ์™ธํ–ˆ๋‹ค.

import Author from "./components/Author.vue";
import Comment from "./components/Comment.vue";

const routes = [
  {
    path: "/detail/:id(\\d+)",
    component: Detail,
    children: [
      {
        path: "author",
        component: Author,
      },
      {
        path: "comment",
        component: Comment,
      },
    ],
  },
];
  • nested routes ์ฝ”๋“œ๋Š” router.js ํŒŒ์ผ์— ์œ„์™€ ๊ฐ™์ด children ์ด๋ผ๋Š” ํ•ญ๋ชฉ์„ ๋งŒ๋“ค์–ด์„œ routes ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.
  • ๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด, /detail/:id/author ๋กœ ์ ‘์†ํ•˜๋ฉด Author.vue ๋ฅผ, /detail/:id/comment ๋กœ ์ ‘์†ํ•˜๋ฉด Comment.vue ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
    (๋ฌผ๋ก  ๊ทธ ์ „์— Author.vue, Comment.vue ์™€ ๊ฐ™์ด ๋ณด์—ฌ์ค„ vue ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ import ๋Š” ํ•ด๋‘์–ด์•ผ ํ•œ๋‹ค.)

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

<template>

  <div class="container mt-4">
    <h4>์ƒ์„ธ ํŽ˜์ด์ง€</h4>
    <h5>{{posts[$route.params.id].title}}</h5>
    <p>{{posts[$route.params.id].content}}</p>
    <p>{{posts[$route.params.id].date}}</p>
  </div>

  <router-view></router-view>
  
</template>
  • ์ด์ œ ์œ„์™€ ๊ฐ™์ด <router-view></router-view> ๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜์— ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด /detail/:id/author, ํ˜น์€ /detail/:id/comment ๋ผ๋Š” ๊ฒฝ๋กœ์— ์ ‘์†ํ–ˆ์„ ๋•Œ, ํ•ด๋‹น ์œ„์น˜์— Author Component, Comment Component ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ’œ ํŽ˜์ด์ง€ ์ด๋™ํ•ด๋ณด๊ธฐ

๐Ÿค $router.push() ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

$router.push('/detail/0')
  • ์œ„์™€ ๊ฐ™์ด $router.push() ํ•จ์ˆ˜ ์•ˆ์— ์ด๋™ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ํŽ˜์ด์ง€ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • <router-link></router-link> ๋Œ€์‹ ์—๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ๋ฐ, ์žฅ์ ์€ ๋ณ€์ˆ˜๋ฅผ ์ง‘์–ด๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿค $router.go() ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

$router.go(-1)
  • ์œ„์™€ ๊ฐ™์ด $router.go() ํ•จ์ˆ˜ ์•ˆ์— ์ˆซ์ž๋ฅผ ๋„ฃ์–ด ์‚ฌ์šฉํ•˜๋ฉด ์•ž์œผ๋กœ๊ฐ€๊ธฐ, ๋’ค๋กœ๊ฐ€๊ธฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด 1 ์ด๋ผ๊ณ  ์“ฐ๋ฉด ์•ž์œผ๋กœ 1๋ฒˆ ๊ฐ€๊ณ , -2 ๋ผ๊ณ  ์“ฐ๋ฉด ๋’ค๋กœ 2๋ฒˆ ๊ฐ€๋Š” ์‹์ด๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•