๐Vue Router
|Vue Router
โVue.js ๊ณต์ ๋ผ์ฐํฐโ
- ๋ผ์ฐํธ(route)์ ์ปดํฌ๋ํธ๋ฅผ ๋งคํํ ํ, ์ด๋ค ์ฃผ์์์ ๋ ๋๋งํ ์ง ์๋ ค์ค
- SPA ์์์ ๋ผ์ฐํ ์ ์ฝ๊ฒ ๊ฐ๋ฐํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณต
|Vue Router Start!
- ํ๋ก์ ํธ ์์ฑ ๋ฐ ์ด๋
vue create my-router-app
cd my-router-app
- Vue Router plugin์ค์น (Vue CLI ํ๊ฒฝ)
vue add router
[์ฃผ์]
๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ ์งํํ๊ณ ์๋ ๋์ค์ ์ถ๊ฐํ๊ฒ ๋๋ฉด App.vue๋ฅผ ๋ฎ์ด์ฐ๋ฏ๋ก, ํ๋ก์ ํธ ๋ด์์ ๋ค์ ๋ช ๋ น์ ์คํํ๊ธฐ ์ ์ ํ์ํ ๊ฒฝ์ฐ ํ์ผ์ ๋ฐฑ์ (์ปค๋ฐ)ํด์ผ ํจ
- commit ์ฌ๋ถ(Yes)
Warn There are uncommitted changes in the current repository, it's recommended to commit or stash them first.
? Still procced? Yes
- History mode ์ฌ์ฉ ์ฌ๋ถ(Yes)
? Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n)
Yes
Vue Router๋ก ์ธํ ๋ณํ


- App.vue ์ฝ๋
- router/index.js ์์ฑ
- views ๋๋ ํ ๋ฆฌ ์์ฑ
|Vue Router โ โindex.jsโ
- ๋ผ์ฐํธ์ ๊ด๋ จ๋ ์ ๋ณด ๋ฐ ์ค์ ์ด ์์ฑ ๋๋ ๊ณณ
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: AboutView,
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
|Vue Router - โrouter-linkโ & โrouter-viewโ
<template>
<div id="app">
<nav>
<router-link :to="{ name: 'home' }">Home</router-link> |
<router-link :to="{ name: 'about' }">About</router-link>
</nav>
<p>
<router-view/>
</p>
</div>
</template>
- <router-link>
- ์ฌ์ฉ์ ๋ค๋น๊ฒ์ด์ ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ์ปดํฌ๋ํธ
- ๋ชฉํ ๊ฒฝ๋ก๋ โtoโ prop์ผ๋ก ์ง์ ๋จ
- HTML5 ํ์คํ ๋ฆฌ ๋ชจ๋์์ router-link๋ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฐจ๋จํ์ฌ ๋ธ๋ผ์ฐ์ ๊ฐ ํ์ด์ง๋ฅผ ๋ค์ ๋ก๋ ํ์ง ์๋๋ก ํจ โ>์๋ก๊ณ ์นจ ์๋จ!
- a ํ๊ทธ์ง๋ง ์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ GET ์์ฒญ์ ๋ณด๋ด๋ a ํ๊ทธ์ ์กฐ๊ธ ๋ค๋ฅด๊ฒ, ๊ธฐ๋ณธ GET ์์ฒญ์ ๋ณด๋ด๋ ์ด๋ฒคํธ๋ฅผ ์ ๊ฑฐํ ํํ๋ก ๊ตฌ์ฑ๋จ
- <router-view>
- ์ฃผ์ด์ง ๋ผ์ฐํธ์ ๋ํด ์ผ์นํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ์ปดํฌ๋ํธ
- ์ค์ component๊ฐ DOM์ ๋ถ์ฐฉ๋์ด ๋ณด์ด๋ ์๋ฆฌ๋ฅผ ์๋ฏธ
- router-link๋ฅผ ํด๋ฆญํ๋ฉด ํด๋น ๊ฒฝ๋ก์ ์ฐ๊ฒฐ๋์ด ์๋ index.js์ ์ ์ํ ์ปดํฌ๋ํธ๊ฐ ์์น
|History mode
- HTML History API๋ฅผ ์ฌ์ฉํด์ router๋ฅผ ๊ตฌํํ ๊ฒ
- ๋ธ๋ผ์ฐ์ ์ ํ์คํ ๋ฆฌ๋ ๋จ๊ธฐ์ง๋ง ์ค์ ํ์ด์ง๋ ์ด๋ํ์ง ์๋ ๊ธฐ๋ฅ์ ์ง์
- ์ฆ, ํ์ด์ง๋ฅผ ๋ค์ ๋ก๋ํ์ง ์๊ณ URL์ ํ์ํ ์ ์์
- SPA์ ๋จ์ ์ค ํ๋์ธ โURL์ด ๋ณ๊ฒฝ๋์ง ์๋๋ค.โ ๋ฅผ ํด๊ฒฐ
|1. Named Routes
- ์ด๋ฆ์ ๊ฐ์ง๋ ๋ผ์ฐํธ
- ๋ช ๋ช ๋ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ vue-router ์ปดํฌ๋ํธ ์์์ prop์ ์ ๋ฌ
//App.vue -- to์์ v-bind!!
<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/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: AboutView,
},
]
|2. ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ค๋น๊ฒ์ด์
- <router-link>๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธ์ ํ์์ ์ํ a ํ๊ทธ๋ฅผ ๋ง๋๋ ๊ฒ ์ธ์๋, router์ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ๊ฐ์ ์์ ์ ์ํํ ์ ์์
์ ์ธ์ ๋ฐฉ์ | ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ |
<router-link to=โโฆโ> | $router.push(โฆ) |
- Vue ์ธ์คํด์ค ๋ด๋ถ์์ ๋ผ์ฐํฐ ์ธ์คํด์ค์ $router๋ก ์ ๊ทผํ ์ ์์
- ๋ฐ๋ผ์ ๋ค๋ฅธ URL๋ก ์ด๋ํ๋ ค๋ฉด this.$router.push๋ฅผ ํธ์ถํ ์ ์์
- ์ด ๋ฉ์๋๋ ์๋ก์ด ํญ๋ชฉ์ ํ์คํ ๋ฆฌ ์คํ์ ๋ฃ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๊ฐ ๋ธ๋ผ์ฐ์ ์ ๋ค๋ก ๊ฐ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ด์ URL๋ก ์ด๋ํ๊ฒ ๋จ
- <router-link>๋ฅผ ํด๋ฆญํ ๋ ๋ด๋ถ์ ์ผ๋ก ํธ์ถ๋๋ ๋ฉ์๋์ด๋ฏ๋ก <router-link :to="...">๋ฅผ ํด๋ฆญํ๋ฉด, **router.push(...)**๋ฅผ ํธ์ถํ๋ ๊ฒ๊ณผ ๊ฐ์
- ์์ฑํ ์ ์๋ ์ธ์ ์์

|3. Dynamic Route Matching
- ๋์ ์ธ์ ์ ๋ฌ
- ์ฃผ์ด์ง ํจํด์ ๊ฐ์ง ๋ผ์ฐํธ๋ฅผ ๋์ผํ ์ปดํฌ๋ํธ์ ๋งคํํด์ผ ํ๋ ๊ฒฝ์ฐ
- ์๋ฅผ ๋ค์ด ๋ชจ๋ User์ ๋ํด ๋์ผํ ๋ ์ด์์์ ๊ฐ์ง์ง๋ง, ๋ค๋ฅธ User ID๋ก ๋ ๋๋ง ๋์ด์ผํ๋ User ์ปดํฌ๋ํธ ์์
//router/index.js
const routes = [
{
path: '/user/:userId',
name: 'User',
component: User
}
]
- ๋์ ์ธ์๋ :(์ฝ๋ก )์ผ๋ก ์์
- ์ปดํฌ๋ํธ์์ this.$route.params๋ก ์ฌ์ฉ๊ฐ๋ฅ
pattern | matched path | $route.params |
/user/:userName | /user/john | { username : โjohnโ } |
/user/:userName/article/:articleId | /user/john/article/12 | { username: โjohnโ,articleId: 12 } |
|components์ views
- ๊ธฐ๋ณธ์ ์ผ๋ก ์์ฑ๋ ๊ตฌ์กฐ์์ components ํด๋์ views ํด๋ ๋ด๋ถ์ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ปดํฌ๋ํธ๊ฐ ์กด์ฌํ๊ฒ ๋จ
- ์ปดํฌ๋ํธ๋ฅผ ์์ฑํด ๊ฐ ๋ ์ ํด์ง ๊ตฌ์กฐ๊ฐ ์๋ ๊ฒ์ ์๋๋ฉฐ, ์ฃผ๋ก ์๋์ ๊ฐ์ด ๊ตฌ์กฐํํ์ฌ ํ์ฉํจ
- App.vue
- ์ต์์ ์ปดํฌ๋ํธ
- views/
- router(index.js)์ ๋งคํ๋๋ ์ปดํฌ๋ํธ๋ฅผ ๋ชจ์๋๋ ํด๋
- ex) App ์ปดํฌ๋ํธ ๋ด๋ถ์ AboutView & HomeView ์ปดํฌ๋ํธ ๋ฑ๋ก
- components/
- router์ ๋งคํ๋ ์ปดํฌ๋ํธ ๋ด๋ถ์ ์์ฑํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ชจ์๋๋ ํด๋
- ex) Home ์ปดํฌ๋ํธ ๋ด๋ถ์ HelloWorld ์ปดํฌ๋ํธ ๋ฑ๋ก
|Vue Router๊ฐ ํ์ํ ์ด์
- SPA ๋ฑ์ฅ ์ด์
- ์๋ฒ๊ฐ ๋ชจ๋ ๋ผ์ฐํ ์ ํต์
- ์์ฒญ ๊ฒฝ๋ก์ ๋ง๋ HTML๋ฅผ ์ ๊ณต
- SPA ๋ฑ์ฅ ์ดํ
- ์๋ฒ๋ index.html ํ๋๋ง ์ ๊ณต
- ์ดํ ๋ชจ๋ ์ฒ๋ฆฌ๋ HTML ์์์ JS ์ฝ๋๋ฅผ ํ์ฉํด ์งํ
- ์ฆ, ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ๋ ์ด์ ์๋ฒ๊ฐ ํ์ง ์์ (ํ ํ์๊ฐ ์์ด์ง)
- ๋ผ์ฐํ
์ฒ๋ฆฌ ์ฐจ์ด
- SSR
- ๋ผ์ฐํ ์ ๋ํ ๊ฒฐ์ ๊ถ์ ์๋ฒ๊ฐ ๊ฐ์ง
- CSR
- ํด๋ผ์ด์ธํธ๋ ๋ ์ด์ ์๋ฒ๋ก ์์ฒญ์ ๋ณด๋ด์ง ์๊ณ ์๋ต๋ฐ์ HTML ๋ฌธ์์์์ ์ฃผ์๊ฐ ๋ณ๊ฒฝ๋๋ฉด ํน์ ์ฃผ์์ ๋ง๋ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง
- ๋ผ์ฐํ ์ ๋ํ ๊ฒฐ์ ๊ถ์ ํด๋ผ์ด์ธํธ๊ฐ ๊ฐ์ง
- ๊ฒฐ๊ตญ Vue Router๋ ๋ผ์ฐํ ์ ๊ฒฐ์ ๊ถ์ ๊ฐ์ง Vue.js์์ ๋ผ์ฐํ ์ ํธ๋ฆฌํ๊ฒ ํ ์ ์๋ Tool์ ์ ๊ณตํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- SSR
'Development > Vue.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue.js - Vuex๋ฅผ ํ์ฉํ Todo App ๊ตฌํํ๊ธฐ (0) | 2022.05.11 |
---|---|
Vue.js - Youtube API๋ฅผ ์ด์ฉํด props์ emit ํ์ฉํ๊ธฐ (0) | 2022.05.11 |
Vue.js - Pass Props & Emit Events (0) | 2022.05.09 |
Vue.js - Babel & Webpack (0) | 2022.05.09 |
Vue.js - Vue CLI (0) | 2022.05.09 |
๋๊ธ