νλ‘κ·Έλλ° λ°©μ λ΄λΉκ²μ΄μ β
μ μΈμ μΌλ‘ λ΄λΉκ²μ΄μ
μ νκΈ° μν΄ <router-link>
λ₯Ό μ¬μ©νμ¬ μ΅μ»€ νκ·Έλ₯Ό λ§λλ κ² μΈμλ, λΌμ°ν°μ μΈμ€ν΄μ€ λ©μλλ₯Ό μ¬μ©νμ¬ νλ‘κ·Έλλ° λ°©μμΌλ‘ λ΄λΉκ²μ΄μ
ν μ μμ΅λλ€.
λ€λ₯Έ μμΉλ‘ μ΄λνκΈ° β
μ°Έκ³ : μλ μμ μμλ λΌμ°ν° μΈμ€ν΄μ€λ₯Ό router
λ‘ μ§μΉν©λλ€. μ»΄ν¬λνΈ λ΄λΆμμλ $router
μμ±μ μ¬μ©νμ¬ λΌμ°ν°μ μ κ·Όν μ μμ΅λλ€. μ: this.$router.push(...)
. Composition APIλ₯Ό μ¬μ©νλ κ²½μ°, useRouter()
λ₯Ό νΈμΆνμ¬ λΌμ°ν°μ μ κ·Όν μ μμ΅λλ€.
λ€λ₯Έ URLλ‘ μ΄λνλ €λ©΄ router.push
λ₯Ό μ¬μ©νμΈμ. μ΄ λ©μλλ νμ€ν 리 μ€νμ μ νλͺ©μ μΆκ°νλ―λ‘, μ¬μ©μκ° λΈλΌμ°μ μ λ€λ‘ κ°κΈ° λ²νΌμ ν΄λ¦νλ©΄ μ΄μ URLλ‘ μ΄λνκ² λ©λλ€.
μ΄ λ©μλλ <router-link>
λ₯Ό ν΄λ¦ν λ λ΄λΆμ μΌλ‘ νΈμΆλλ―λ‘, <router-link :to="...">
λ₯Ό ν΄λ¦νλ κ²μ router.push(...)
λ₯Ό νΈμΆνλ κ²κ³Ό λμΌν©λλ€.
μ μΈμ | νλ‘κ·Έλλ° λ°©μ |
---|---|
<router-link :to="..."> | router.push(...) |
μΈμλ λ¬Έμμ΄ κ²½λ‘λ μμΉ μ€λͺ κ°μ²΄κ° λ μ μμ΅λλ€. μμ:
// 리ν°λ΄ λ¬Έμμ΄ κ²½λ‘
router.push('/users/eduardo')
// κ²½λ‘κ° μλ κ°μ²΄
router.push({ path: '/users/eduardo' })
// λΌμ°ν°κ° URLμ μμ±ν μ μλλ‘ νλΌλ―Έν°κ° μλ μ΄λ¦ μλ λΌμ°νΈ
router.push({ name: 'user', params: { username: 'eduardo' } })
// 쿼리μ ν¨κ», κ²°κ³Όλ /register?plan=private
router.push({ path: '/register', query: { plan: 'private' } })
// ν΄μμ ν¨κ», κ²°κ³Όλ /about#team
router.push({ path: '/about', hash: '#team' })
μ°Έκ³ : path
κ° μ 곡λλ©΄ params
λ 무μλλ©°, μ μμμμ λ³Ό μ μλ―μ΄ query
λ κ·Έλ μ§ μμ΅λλ€. λμ , λΌμ°νΈμ name
μ μ 곡νκ±°λ λͺ¨λ νλΌλ―Έν°λ₯Ό ν¬ν¨ν μ 체 path
λ₯Ό μλμΌλ‘ μ§μ ν΄μΌ ν©λλ€:
const username = 'eduardo'
// URLμ μλμΌλ‘ λ§λ€ μ μμ§λ§ μΈμ½λ©μ μ§μ μ²λ¦¬ν΄μΌ ν©λλ€
router.push(`/user/${username}`) // -> /user/eduardo
// λμΌ
router.push({ path: `/user/${username}` }) // -> /user/eduardo
// κ°λ₯νλ€λ©΄ μλ URL μΈμ½λ©μ μ΄μ μ μν΄ `name`κ³Ό `params`λ₯Ό μ¬μ©νμΈμ
router.push({ name: 'user', params: { username } }) // -> /user/eduardo
// `params`λ `path`μ ν¨κ» μ¬μ©ν μ μμ΅λλ€
router.push({ path: '/user', params: { username } }) // -> /user
params
λ₯Ό μ§μ ν λλ λ°λμ string
λλ number
(λλ λ°λ³΅ κ°λ₯ν νλΌλ―Έν°λ₯Ό μν μ΄λ€μ λ°°μ΄)λ₯Ό μ 곡ν΄μΌ ν©λλ€. κ·Έ μΈμ νμ
(κ°μ²΄, λΆλ¦¬μΈ λ±)μ μλμΌλ‘ λ¬Έμμ΄λ‘ λ³νλ©λλ€. μ νμ νλΌλ―Έν°μ κ²½μ°, κ°μ λΉ λ¬Έμμ΄(""
)μ΄λ null
λ‘ μ 곡νμ¬ μ κ±°ν μ μμ΅λλ€.
to
propμ router.push
μ λμΌν μ’
λ₯μ κ°μ²΄λ₯Ό νμ©νλ―λ‘, λμΌν κ·μΉμ΄ λͺ¨λ μ μ©λ©λλ€.
router.push
λ° λ€λ₯Έ λͺ¨λ λ΄λΉκ²μ΄μ
λ©μλλ _Promise_λ₯Ό λ°ννλ―λ‘, λ΄λΉκ²μ΄μ
μ΄ μλ£λ λκΉμ§ κΈ°λ€λ¦¬κ±°λ μ±κ³΅ λλ μ€ν¨ μ¬λΆλ₯Ό μ μ μμ΅λλ€. μ΄μ λν΄μλ λ΄λΉκ²μ΄μ
μ²λ¦¬μμ λ μμΈν λ€λ£Ήλλ€.
νμ¬ μμΉ λ체νκΈ° β
μ΄ λ©μλλ router.push
μ λΉμ·νκ² λμνμ§λ§, μ΄λ¦μμ μ μ μλ―μ΄ μλ‘μ΄ νμ€ν 리 νλͺ©μ μΆκ°νμ§ μκ³ νμ¬ νλͺ©μ λ체ν©λλ€.
μ μΈμ | νλ‘κ·Έλλ° λ°©μ |
---|---|
<router-link :to="..." replace> | router.replace(...) |
router.push
μ μ λ¬λλ to
μΈμμ replace: true
μμ±μ μ§μ μΆκ°νλ κ²λ κ°λ₯ν©λλ€:
router.push({ path: '/home', replace: true })
// μλμ λμΌ
router.replace({ path: '/home' })
νμ€ν 리 μ΄λ β
μ΄ λ©μλλ νλμ μ μν νλΌλ―Έν°λ₯Ό λ°μ, νμ€ν 리 μ€νμμ μμΌλ‘ λλ λ€λ‘ λͺ λ¨κ³ μ΄λν μ§ μ§μ ν©λλ€. μ΄λ window.history.go(n)
κ³Ό μ μ¬ν©λλ€.
μμ
// ν λ¨κ³ μμΌλ‘ μ΄λ, router.forward()μ λμΌ
router.go(1)
// ν λ¨κ³ λ€λ‘ μ΄λ, router.back()κ³Ό λμΌ
router.go(-1)
// 3λ¨κ³ μμΌλ‘ μ΄λ
router.go(3)
// ν΄λΉ λ¨κ³λ§νΌ κΈ°λ‘μ΄ μμΌλ©΄ μ‘°μ©ν μ€ν¨ν¨
router.go(-100)
router.go(100)
νμ€ν 리 μ‘°μ β
router.push
, router.replace
, router.go
κ° window.history.pushState
, window.history.replaceState
, window.history.go
μ λμ λ©μλλΌλ μ μ λμΉμ±μ μ μμ΅λλ€. μ΄λ€μ window.history
APIλ₯Ό λͺ¨λ°©ν©λλ€.
λ°λΌμ λΈλΌμ°μ νμ€ν 리 APIμ μ΄λ―Έ μ΅μνλ€λ©΄, Vue Routerλ₯Ό μ¬μ©ν λ νμ€ν 리 μ‘°μμ΄ μ΅μνκ² λκ»΄μ§ κ²μ λλ€.
λν Vue Routerμ λ΄λΉκ²μ΄μ
λ©μλ(push
, replace
, go
)λ λΌμ°ν° μΈμ€ν΄μ€λ₯Ό μμ±ν λ μ λ¬λ history
μ΅μ
κ³Ό μκ΄μμ΄ μΌκ΄λκ² λμνλ€λ μ λ μΈκΈν κ°μΉκ° μμ΅λλ€.