λΌμ°νΈ μ μ¬μ©νκΈ°
μ΄λ¦μ κ°μ§λ λΌμ°νΈ
- 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λ₯Ό λΆμ
λλ€) μ΅μ
μ μ¬μ©ν΄μΌν©λλ€.
<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>
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: [
{
path: 'profile',
component: UserProfile
},
{
path: 'posts',
component: UserPosts
},
{
path: '', component: UserHome
},
]
}
]
})