πŸ€– Vue.js λΌμš°νŒ…μ— λŒ€ν•΄ μ•Œμ•„λ³΄μž (2)

YOU KNOW I MEANΒ·2021λ…„ 3μ›” 18일
0
post-thumbnail
post-custom-banner

라우트 잘 μ‚¬μš©ν•˜κΈ°

이름을 κ°€μ§€λŠ” 라우트

  • routes μ˜΅μ…˜μ— 이름을 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
  • /user/123 경둜둜 μ΄λ™ν•©λ‹ˆλ‹€.
// μ •μ˜
const router = new VueRouter({
  routes: [
    {
      path: '/user/:userId',
      name: 'user',
      component: User
    }
  ]
})

// 선언적 μ‚¬μš©
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
// ν”„λ‘œκ·Έλž˜λ°μ  μ‚¬μš©
router.push({ name: 'user', params: { userId: 123 }})

이름을 κ°€μ§€λŠ” λ·°

  • μ—¬λŸ¬ 개의 λ·°λ₯Ό μ€‘μ²©ν•˜μ§€ μ•Šκ³  λ™μ‹œμ— ν‘œμ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • μ—¬λŸ¬ λ·°μ—λŠ” λ™μΌν•œ λΌμš°νŠΈμ— λŒ€ν•΄ μ—¬λŸ¬ μ»΄ν¬λ„ŒνŠΈκ°€ ν•„μš”ν•©λ‹ˆλ‹€.
  • components(sλ₯Ό λΆ™μž…λ‹ˆλ‹€) μ˜΅μ…˜μ„ μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.
// Main.vue
<router-view class="view one" name="header"></router-view>
<router-view class="view two"></router-view>
<router-view class="view three" name="footer"></router-view>

// router/index.js
const router = new VueRouter({
  routes: [
    {
      path: '/main',
      components: {
        default: Main,
        header: Header,
        footer: Footer
      }
    }
  ]
})

동적 라우트 맀칭

  • 주어진 νŒ¨ν„΄μ„ 가진 라우트λ₯Ό λ™μΌν•œ μ»΄ν¬λ„ŒνŠΈμ— λ§€ν•‘ν•΄μ•Όν•˜λŠ” κ²½μš°κ°€ 자주 μžˆμŠ΅λ‹ˆλ‹€.
    • board/1 , board/2 와 같은 λΌμš°νŠΈμ— μ ‘κ·Όν•  λ•Œ λΌμš°νŠΈμ— 맀칭된 κ²Œμ‹œνŒ 번호λ₯Ό μ»΄ν¬λ„ŒνŠΈμ— λ‚˜νƒ€λ‚Ό λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
  • 동적 μ„Έκ·Έλ¨ΌνŠΈλŠ” 콜둠 : 으둜 ν‘œμ‹œλ©λ‹ˆλ‹€.
  • λΌμš°νŠΈκ°€ μΌμΉ˜ν•˜λ©΄ 동적 μ„Έκ·Έλ¨ΌνŠΈμ˜ 값은 λͺ¨λ“  μ»΄ν¬λ„ŒνŠΈμ—μ„œ this.$route.params 둜 ν‘œμ‹œλ©λ‹ˆλ‹€.
const Board = {
  template: '<div>κ²Œμ‹œνŒ 번호 {{ $route.params.no }}</div>'
}

const router = new VueRouter({
  routes: [
    // 동적 μ„Έκ·Έλ¨ΌνŠΈλŠ” 콜둠으둜 μ‹œμž‘ν•©λ‹ˆλ‹€.
    { path: '/board/:no', component: Board }
  ]
})

μ½”λ“œ μ˜ˆμ‹œ λ°”λ‘œκ°€κΈ°


μ€‘μ²©λœ 라우트

/user/foo/profile                     /user/foo/posts
+------------------+                  +-----------------+
| User             |                  | User            |
| +--------------+ |                  | +-------------+ |
| | Profile      | |  +------------>  | | Posts       | |
| |              | |                  | |             | |
| +--------------+ |                  | +-------------+ |
+------------------+                  +-----------------+
  • Profileκ³Ό Posts에 μ ‘κ·Όν•˜λ €λ©΄ Children 속성을 μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.
const User = {
  template: `
    <div class="user">
      <h2>User {{ $route.params.id }}</h2>
      <router-view></router-view>
    </div>
  `
} 

const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User,
      children: [
        {
          // /user/:id/profile κ³Ό 일치 ν•  λ•Œ
          // UserProfile은 User의 <router-view> 내에 λ Œλ”λ§ λ©λ‹ˆλ‹€.
          path: 'profile',
          component: UserProfile
        },
        {
          // /user/:id/posts κ³Ό 일치 ν•  λ•Œ
          // UserPostsκ°€ User의 <router-view> 내에 λ Œλ”λ§ λ©λ‹ˆλ‹€.
          path: 'posts',
          component: UserPosts
        },
				{
					// UserHome은 /user/:id κ°€ 일치 ν•  λ•Œ
	        // User의 <router-view> μ•ˆμ— λ Œλ”λ§λ©λ‹ˆλ‹€.
	        path: '', component: UserHome 
				},
      ]
    }
  ]
})

post-custom-banner

0개의 λŒ“κΈ€