// Vue 프로젝트 생성
$ vue create vue-router-app
// 디렉토리 이동
$ cd vue-router-app
// Vue CLI를 통해 router plugin 적용
$ vue add router
<template>
<div id="app">
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view/>
</div>
</template>
to
’ 속성으로 지정됨App.vue
는 base.html의 역할router-view
는 block 태그로 감싼 부분// App.vue
<template>
<div id="app">
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view/>
</div>
</template>
// router/index.js
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
]
// 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
로 접근 할 수 있음this.$router.push
를 사용<router-link :to="...">
를 클릭하는 것과 $router.push(...)
를 호출하는 것은 같은 동작// AboutView.vue
<template>
<div class="about">
<h1>This is an about page</h1>
<router-link :to="{ name: 'home'}">Home</router-link> |
<button @click="toHome">홈으로!!!</button>
</div>
</template>
<script>
export default {
name: 'AboutView',
methods: {
toHome() {
this.$router.push({name: 'home'})
}
}
}
</script>
// router/index.js
import HelloView from '../views/HelloView.vue'
Vue.use(VueRouter)
const routes = [
...,
{
path: '/hello/:userName',
name: 'hello',
component: HelloView
}
]
// views/HelloView.vue
<template>
<div>
</div>
</template>
<script>
export default {
name: 'HellowView',
}
</script>
<style>
</style>
$route.params
로 변수에 접근 가능// views/HelloView.vue
<template>
<div>
<h1>hello, {{ $route.params.userName }}</h1>
</div>
</template>
<script>
export default {
name: 'HellowView',
}
</script>
// views/HelloView.vue
<template>
<div>
<h1>hello, {{ userName }}</h1>
</div>
</template>
<script>
export default {
name: 'HellowView',
data() {
return {
userName: this.$route.params.userName
}
}
}
</script>
// App.vue
<template>
<div id="app">
<nav>
<router-link :to="{ name: 'home' }">Home</router-link> |
<router-link :to="{ name: 'about' }">About</router-link> |
<!-- params를 이용하여 동적 인자 전달 가능 -->
<router-link :to="{ name: 'hello' , params: { userName : 'jieun' }}">Hello</router-link>
</nav>
<router-view/>
</div>
</template>
// AboutView.vue
<template>
<div class="about">
...,
<!-- AboutView에서 데이터를 입력 받아 HelloView로 이동 -->
<input type="text" @keyup.enter="goToHello" v-model="inputData">
</div>
</template>
<script>
export default {
name: 'AboutView',
data() {
return {
inputData: null,
}
},
methods: {
...,
goToHello() {
this.$router.push({name: 'hello', params: { userName: this.inputData}})
}
}
}
</script>
// router/index.js
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
]
// router/index.js
const routes = [
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue'),
}
]
const routes = [
{
path: '/hello',
name: 'hello',
component: () => import(/* webpackChunkName: "about" */ '../views/HelloView.vue'),
}
]
https://github.com/mjieun0956/TIL/tree/master/Vue/10.%20Vue%20Router/vue-router-app