Vue Router
Routing
Routing
โ ๋คํธ์ํฌ์์ ๊ฒฝ๋ก๋ฅผ ์ ํํ๋ ํ๋ก๋ ์ค
โ ์น ์๋น์ค์์์ ๋ผ์ฐํ
: ์ ์ ๊ฐ ๋ฐฉ๋ฌธํ URL์ ๋ํ ์ ์ ํ ๊ฒฐ๊ณผ๋ฅผ ์๋ตํ๋ ๊ฒ
Routing in SSR
โ Server๊ฐ ๋ชจ๋ ๋ผ์ฐํ
์ ํต์
โ URL๋ก ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์๋ต์ผ๋ก ์์ฑ๋ HTML ์ ๊ณต
- Django๋ก ๋ณด๋ธ ์์ฒญ์ ์๋ต HTML์ ์์ฑ๋ณธ์ธ ์ํ
โ ๊ฒฐ๋ก ์ ์ผ๋ก, Routing(URL)์ ๋ํ ๊ฒฐ์ ๊ถ์ ์๋ฒ๊ฐ ๊ฐ์ง๋ค.
Routing in SPA / CSR
โ ์๋ฒ๋ ํ๋์ HTML(index.html)๋ง์ ์ ๊ณต
โ ์ดํ์ ๋ชจ๋ ๋์์ ํ๋์ HTML ๋ฌธ์ ์์์ JavaScript ์ฝ๋ ํ์ฉ
- DOM์ ๊ทธ๋ฆฌ๋๋ฐ ํ์ํ ์ถ๊ฐ์ ์ธ ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด axios์ ๊ฐ์ AJAX ์์ฒญ์ ๋ณด๋ผ ์ ์๋ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ฒ๋ฆฌ
โ ์ฆ ํ๋์ URL๋ง ๊ฐ์ง๋ค.
Why routing?
โ ๋์์ ๋ฐ๋ผ URL์ด ๊ผญ ๋ฐ๋ ํ์๋ ์์ผ๋ ์ ์ ์ ์ฌ์ฉ์ฑ ๊ด์ ์์๋ ํ์ํ๋ค
โ Routing์ด ์๋ค๋ฉด
- ์ ์ ๊ฐ URL์ ํตํ ํ์ด์ง์ ๋ณํ ๊ฐ์ง ๋ถ๊ฐ
- ํ์ด์ ๊ฐ ๋ฌด์์ ๋ ๋๋ง ์ค์ธ์ง์ ๋ํ ์ํ๋ฅผ ์ ์ ์๋ค.
- ๋ค๋ก ๊ฐ๊ธฐ ๊ธฐ๋ฅ ์ฌ์ฉ ๋ถ๊ฐ
Vue Router
Vue Router
โ Vue์ ๊ณต์ ๋ผ์ฐํฐ
โ SPA ์์์ ๋ผ์ฐํ
์ ์ฝ๊ฒ ๊ฐ๋ฐํ ์ ์๋ ๊ธฐ๋ฅ ์ ๊ณต
โ ๋ผ์ฐํธ(routes)์ ์ปดํฌ๋ํธ๋ฅผ ๋งคํํ ํ, ์ด๋ค URL์์ ๋ ๋๋ง ํ ์ง ์๋ ค์ค๋ค
โ SPA๋ฅผ MPA์ฒ๋ผ URL์ ์ด๋ํ๋ฉด์ ์ฌ์ฉ ๊ฐ๋ฅ
Vue Router ์์ํ๊ธฐ
$ vue create vue-router-app
$ cd vue-router-app
$ vue add router
![](https://blog.kakaocdn.net/dn/cvebqH/btrQQiWjFRL/IMczzb233pWDrFN8mzlOMK/img.png)
โ History Mode: Y
History mode
โ ๋ธ๋ผ์ฐ์ ์ History API๋ฅผ ํ์ฉํ ๋ฐฉ์
- ์๋ก ๊ณ ์นจ ์์ด URL ์ด๋ ๊ธฐ๋ก์ ๋จ๊ธธ ์ ์๋ค.
โ ์ฐ๋ฆฌ์๊ฒ ์ต์ํ URL ๊ตฌ์กฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
- ex)
http://localhost:8080/index
![](https://blog.kakaocdn.net/dn/byS5Qi/btrQOjIUixo/OoJz0zRrnjcpTZQhZeyEQK/img.png)
โ router/index.js ์์ฑ
โ views ํด๋ ์์ฑ
router-link
โ aํ๊ทธ์ ๋น์ทํ ๊ธฐ๋ฅ -> URL ์ด๋
- routes์ ๋ฑ๋ก๋ ์ปดํฌ๋ํธ์ ๋งคํ
- ํ์คํ ๋ฆฌ์ ๋ชจ๋์์ router-link๋ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฐจ๋จํ์ฌ aํ๊ทธ์ ๋ฌ๋ฆฌ ๋ธ๋ผ์ฐ์ ๊ฐ ํ์ด์ง๋ฅผ ๋ค์ ๋ก๋ํ์ง ์๋๋ก ํ๋ค
โ ๋ชฉํ ๊ฒฝ๋ก๋ 'to'
์์ฑ์ผ๋ก ์ง์
router-view
โ ์ฃผ์ด์ง URL์ ๋ํด ์ผ์นํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ์ปดํฌ๋ํธ
โ ์ค์ component๊ฐ DOM์ ๋ถ์ฐฉ๋์ด ๋ณด์ด๋ ์๋ฆฌ ์๋ฏธ
โ router-link๋ฅผ ํด๋ฆญํ๋ฉด routes์ ๋งคํ๋ ์ปดํฌ๋ํธ ๋ ๋๋ง
โ Django์์์ block tag์ ์ ์ฌ
- App.vue๋ base.html ์ญํ
- router-view๋ block ํ๊ทธ๋ก ๊ฐ์ผ ๋ถ๋ถ
src/router/index.js
// src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
โ ๋ผ์ฐํฐ์ ๊ด๋ จ๋ ์ ๋ณด ๋ฐ ์ค์ ์ด ์์ฑ๋๋ ๊ณณ
โ Django์ urls.py์ ํด๋น
โ routes์ URL์ ์ปดํฌ๋ํธ ๋งคํ
src/Views
โ router-view์ ๋ค์ด๊ฐ component ์์ฑ
โ ๊ธฐ์กด์ ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ๋ ๊ณณ์ componet ํด๋๋ฟ์ด์์ง๋ง ์ด์ ๋ ํด๋๋ก ๋๋์ด์ง๋ค
โ ๊ฐ ํด๋ ์์ .vue ํ์ผ๋ค์ด ๊ธฐ๋ฅ์ ์ผ๋ก ๋ค๋ฅธ ๊ฒ์ ์๋๋ค
ํด๋๋ณ ์ปดํฌ๋ํธ ๋ฐฐ์น(๊ท์ฝ์ ์๋๋ค)
โ views/
- routes์ ๋งคํ๋๋ ์ปดํฌ๋ํธ, ์ฆ ์ ์์น์ ๋ ๋๋ง ๋๋ ์ปดํฌ๋ํธ๋ฅผ ๋ชจ์๋๋ ํด๋
- ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ๊ตฌ๋ถํ๊ธฐ ์ํด View๋ก ๋๋๋๋ก ๋ง๋๋ ๊ฒ ๊ถ์ฅ
โ components/
- routes์ ๋งคํ๋ ์ปดํฌ๋ํธ์ ํ์ ์ปดํฌ๋ํธ๋ฅผ ๋ชจ์๋๋ ํด๋
- HomeView ์ปดํฌ๋ํธ ๋ด๋ถ์ HelloWorld ์ปดํฌ๋ํธ
Vue Router ์ค์ต
์ฃผ์ ์ด๋ 2๊ฐ์ง ๋ฐฉ๋ฒ
- ์ ์ธ์ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
- ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
1. ์ ์ธ์ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
// App.vue
<template>
<div id="app">
<nav>
<router-link :to="{ name: 'home'}">Home</router-link> |
<router-link :to="{ name: 'about' }">About</router-link>
</nav>
<router-view/>
</div>
</template>
โ router-link์ 'to'
์์ฑ์ผ๋ก ์ฃผ์ ์ ๋ฌ
โ ๋์ ์ธ ๊ฐ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ v-bind ์ฌ์ฉ
ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
<template>
<div class="about">
<h1>This is an about page</h1>
<button @click="toHome">ํ์ผ๋ก!</button>
</div>
</template>
<script>
export default {
name: 'AboutView',
methods: {
toHome() {
this.$router.push({name: 'home'})
}
},
}
</script>
โ Vue ์ธ์คํด์ค ๋ด๋ถ์์ ๋ผ์ฐํฐ ์ธ์คํด์ค์์ $router
๋ก ์ ๊ทผ ๊ฐ๋ฅ
โ ๋ค๋ฅธ URL๋ก ์ด๋ํ๊ฒจ๋ฉด this.$router.push
์ฌ์ฉ
- history stack์ ์ด๋ํ URL์ ๋ฃ๋ ๋ฐฉ์
- history stack์ ๊ธฐ๋ก์ด ๋จ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๊ฐ ๋ธ๋ผ์ฐ์ ์ ๋ค๋ก ๊ฐ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ด์ URL๋ก ์ด๋ ๊ฐ๋ฅ
โ <router-link :to='...'>
๋ฅผ ํด๋ฆญํ๋ ๊ฒ๊ณผ $router.push(...)
๋ฅผ ํธ์ถํ๋ ๊ฒ๊ณผ ๊ฐ์ ๋์
Dynamic Route Matching
// router/index.js
import HelloView from '@/views/HelloView'
Vue.use(VueRouter)
const routes = [
...
{
path: '/hello/:username',
name: 'hello',
component: HelloView
}
]
// views/HelloView.vue
<template>
<div></div>
</template>
<script>
export default {
name: 'HelloView'
}
</script>
<style>
</style>
โ $route.params
๋ก ๋ณ์์ ์ ๊ทผ ๊ฐ๋ฅ
// views/HelloView.vue
<template>
<div>
<h1>hello, {{ $route.params.userName }}</h1>
</div>
</template>
<script>
export default {
name: 'HelloView'
}
</script>
<style>
</style>
โ ๋จ, HTML์์ ์ง์ ์ฌ์ฉํ๊ธฐ๋ณด๋ค๋ data์ ๋ฃ์ด์ ์ฌ์ฉํ๋ ๊ฒ ๊ถ์ฅ
<template>
<div>
<h1>hello, {{ userName }}</h1>
</div>
</template>
<script>
export default {
name: 'HelloView',
data() {
return {
userName: this.$route.params.userName
}
},
}
</script>
<style>
</style>
Dynamic Route Matching - ์ ์ธ์ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
// App.vue
<template>
<div id="app">
<nav>
<router-link :to="{ name: 'home'}">Home</router-link> |
<router-link :to="{ name: 'about' }">About</router-link> |
<router-link :to="{ name: 'hello', params: { userName: 'sunjun' }}">Hello</router-link>
</nav>
<router-view/>
</div>
</template>
โ App.vue์์ sunjun์ผ๋ก ์ธ์ฌํ๋ ํ์ด์ง๋ก ์ด๋
โ params ์ด์ฉํ์ฌ ๋์ ์ธ์ ์ ๋ฌ ๊ฐ๋ฅ
Dynamic Route Matching - ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
// AboutView.vue
<template>
<div class="about">
<h1>This is an about page</h1>
<button @click="toHome">ํ์ผ๋ก!</button>
<input
type="text"
@keyup.enter="goToHello"
v-model='inputData'
>
</div>
</template>
<script>
export default {
name: 'AboutView',
data() {
return {
inputData : null
}
},
methods: {
toHome() {
this.$router.push({name: 'home'})
},
goToHello() {
this.$router.push({ name: 'hello', params: { userName: this.inputData }})
}
},
}
</script>
![](https://blog.kakaocdn.net/dn/Gv0IV/btrQRmEePpl/B6L3QYGk9GDFkhkfOl4Y8K/img.png)
![](https://blog.kakaocdn.net/dn/pdaAV/btrQPLqUvkd/pSGhgkB44qBp9GFPvc2UWK/img.png)
route์ ์ปดํฌ๋ํธ๋ฅผ ๋ฑ๋กํ๋ ๋๋ค๋ฅธ ๋ฐฉ๋ฒ
![](https://blog.kakaocdn.net/dn/1q9VG/btrQOy6Rrm6/pzhio4D5Zy0T0QgqaNFEJk/img.png)
โ router/index.js์ ์ปดํฌ๋ํธ๋ฅผ ๋ฑ๋กํ๋ ๋๋ค๋ฅธ ๋ฐฉ์
lazy-loading (์ง์ฐ ๋ก๋ฉ)
โ ๋ชจ๋ ํ์ผ์ ํ ๋ฒ์ ๋ก๋ํ๋ ค๊ณ ํ๋ฉด ๋ชจ๋ ๊ฑธ ๋ค ์ฝ๋ ์๊ฐ์ด ๋งค์ฐ ์ค๋ ๊ฑธ๋ฆฐ๋ค
โ ๋ฏธ๋ฆฌ ๋ก๋ํ์ง ์๊ณ ํน์ ๋ผ์ฐํธ์ ๋ฐฉ๋ฌธํ ๋ ๋งคํ๋ ์ปดํฌ๋ํธ์ ์ฝ๋๋ฅผ ๋ก๋ํ๋ ๋ฐฉ์์ ํ์ฉํ ์ ์์
- ๋น์ฅ ์ฌ์ฉํ์ง ์์ ์ปดํฌ๋ํธ๋ ๋จผ์ ๋ก๋ํ์ง ์๋๋ค
'โญ Personal_Study > Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Article with Vue (0) | 2022.11.19 |
---|---|
Navigation Guard (0) | 2022.11.19 |
UX & UI (0) | 2022.11.18 |
Todo: Local Storage (0) | 2022.11.16 |
Vuex ํ์ฉํด Todo SPA ์ฑ ๋ง๋ค๊ธฐ (0) | 2022.11.15 |
๋๊ธ