λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ“  archive

Vue.js 쀑첩 λΌμš°ν„° (Nested Router) μ‚¬μš©ν•˜κΈ°

by HandHand 2021. 3. 1.

μ›ν•˜λŠ” URL νŒ¨ν„΄

μ‡Όν•‘λͺ° ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λ©΄μ„œ λ‹€μˆ˜μ˜ μ»΄ν¬λ„ŒνŠΈλ‘œ 이루어진 ν™”λ©΄ λžœλ”λ§μ΄ ν•„μš”ν•œ 상황이 μƒκ²ΌμŠ΅λ‹ˆλ‹€.

μœ μ € κ΄€λ ¨ 정보듀을 μœ„ν•œ λŒ€μ‹œλ³΄λ“œλ₯Ό κ΅¬ν˜„ν•˜λ©΄μ„œ ν™”λ©΄ 섀계λ₯Ό λ‹€μŒκ³Ό 같이 ν–ˆμŠ΅λ‹ˆλ‹€.

/board /board/wishlist  
+------------------+ +-----------------+  
| board | | board |  
| +--------------+ | | +-------------+ |  
| | main | | +------------> | | wishlist | |  
| | | | | | | |  
| +--------------+ | | +-------------+ |  
+------------------+ +-----------------+  

이λ₯Ό μœ„ν•΄μ„œλŠ” νŠΉμ • URLλ³„λ‘œ 화면을 κ΅¬μ„±ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λžœλ”λ§ν•  수 μžˆλ„λ‘ ν•΄μ•Όν•©λ‹ˆλ‹€.

λ‹€ν–‰νžˆλ„ Vue.js μ—μ„œλŠ” 이λ₯Ό μœ„ν•œ 쀑첩 λΌμš°νŒ… 을 μ§€μ›ν•˜κ³  μžˆμœΌλ‹ˆ 이λ₯Ό 잘 ν™œμš©ν•˜κΈ°λ§Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ€‘μ²©λœ λΌμš°ν„°

μ€‘μ²©λœ λΌμš°ν„°λŠ” μ—¬λŸ¬ λ‹¨κ³„λ‘œ 쀑첩 된 μ»΄ν¬λ„ŒνŠΈλ₯Ό 닀루기 μœ„ν•΄ Vue.jsμ—μ„œ μ œκ³΅ν•˜λŠ” λΌμš°νŒ… κΈ°λŠ₯μž…λ‹ˆλ‹€.

κ°œλ°œμžλŠ” 이λ₯Ό 톡해 νŠΉμ • URL νŒ¨ν„΄μ— 따라 μ›ν•˜λŠ” μžμ‹ μ»΄ν¬λ„ŒνŠΈλ₯Ό λžœλ”λ§ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μžμ‹ μ»΄ν¬λ„ŒνŠΈ μ •μ˜

μš°μ„  λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈ ν•˜μœ„μ— λžœλ”λ§λ  μžμ‹ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ •μ˜ν•©λ‹ˆλ‹€.

components/DashBoard.vue

  <template>
    <div>
      λŒ€μ‹œλ³΄λ“œ 메인
    </div>
  </template>

  <script>
  export default {}
  </script>

  <style>
  </style>

components/WishList.vue

  <template>
    <div>
      ss
    </div>
  </template>

  <script>
  export default {}
  </script>

  <style>
  </style>

λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈ μ •μ˜

λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œλŠ” <router-view/> λ₯Ό 톡해 μžμ‹ μ»΄ν¬λ„ŒνŠΈλ₯Ό μ›ν•˜λŠ” μœ„μΉ˜μ— λžœλ”λ§ν•©λ‹ˆλ‹€.

views/DashBoardView.vue

  <template>
    <div>
      λŒ€μ‹œλ³΄λ“œ<br>
      <router-view></router-view>
    </div>
  </template>

  <script>
  export default {}
  </script>

  <style>
  </style>

μ»΄ν¬λ„ŒνŠΈ λ‘œλ“œ 및 λΌμš°ν„° μ •μ˜

이제 μ•žμ„œ μ •μ˜ν•œ μ»΄ν¬λ„ŒνŠΈλ₯Ό λΆˆλŸ¬μ™€ 각각의 URL νŒ¨ν„΄μ— λ§žλ„λ‘ μ •μ˜ν•΄μ€λ‹ˆλ‹€.

μžμ‹ μ»΄ν¬λ„ŒνŠΈλ₯Ό λžœλ”λ§ν•˜κΈ° μœ„ν•΄μ„œλŠ” children 속성을 ν™œμš©ν•΄μ•Όν•©λ‹ˆλ‹€.

children 속성 ν•˜μœ„μ— μ›ν•˜λŠ” μžμ‹ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ •μ˜ν•˜κ³  ν•΄λ‹Ή URL νŒ¨ν„΄μ„ λ§€μΉ˜μ‹œμΌœμ€λ‹ˆλ‹€.

μ΄λ•Œ path: ""λŠ” /dashboard/와 같이 μžμ‹ μ»΄ν¬λ„ŒνŠΈκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ€ URL일 경우λ₯Ό μœ„ν•œ λΌμš°νŒ…μž…λ‹ˆλ‹€.

routes/index.js

import DashBoardView from "../views/DashBoardView.vue";

// sub component for Dashboard
import DashBoard from "../components/DashBoard.vue";
import WishList from "../components/WishList.vue";

Vue.use(VueRouter);

export const router = new VueRouter({
  routes: [
    {
      // ν™ˆ ν™”λ©΄
      path: "/",
      component: MainView,
    },
    {
      // μœ μ € λŒ€μ‹œλ³΄λ“œ
      path: "/dashboard",
      component: DashBoardView,
      children: [
        {
          // μžμ‚­ μ»΄ν¬λ„ŒνŠΈ 없을 경우
          path: "",
          component: DashBoard,
        },
        {
          // μœ„μ‹œ 리슀트
          path: "wishlist",
          component: WishList,
        },
        {
          // ... 기타 child μ»΄ν¬λ„ŒνŠΈ μ •μ˜
        },
      ],
    },
  ],
});

참고 자료

λ°˜μ‘ν˜•

πŸ’¬ λŒ“κΈ€