"Vue.js 공식 라우터"
라우트(route)에 컴포넌트를 매핑한 후, 어떤 주소를 렌더링할 지 알려줌
SPA 상에서 라이퉁을 쉽게 개발할 수 있는 기능을 제공
router
$ vue create my-router-app
$ cd my-router-app
$vue add router
<template>
<div id="app">
<nav>
<!-- <router-link to="/">Home</router-link> | -->
<!-- namedroute
<router-link :to="{name:'home'}">Home</router-link> |
<router-link :to="{name:'about'}">About</router-link> | -->
<router-link :to="{name:'lotto', params: {lottoNum:6}}">Lotto</router-link> |
<router-link to="/lunch">Lunch</router-link>
</nav>
<router-view/>
</div>
</template>
router/index.js 생성
views 디렉토리 생성
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')
},
]
<router-link to="/">Home</router-link>
//index.js
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
},
//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>
선언적 방식 | 프로그래밍 방식 |
---|---|
\ | $router.push(...) |
Vue 인스턴스 내부에서 라우터 인스턴스에 $router로 접근할 수 있음
따라서 다른 URL로 이동하려면 this.$router.push를 호출 할 수 있음
\를 클릭할 때 내부적으로 호출되는 메서드이므로 \를 클릭하면, router.push(...)를 호출하는 것과 같음
작성할 수 있는 인자 예시
// literal string path
router.push('home')
// object
router.push({path:'home'})
//named route
router.push({ name: 'user', params:{userId : '123'}})
// with query, resulting in /register?plan=private
router.push({path: 'register', query:{plan:'private'}})
<template>
<div class="about">
<h1>This is an about page</h1>
<button @clikc="moveToHome">
Home으로 이동
</button>
</div>
</template>
<script>
export default {
name : 'AboutView',
methods : {
moveToHome :fucntion() {
//this.$router.push('/')
this.$router.push({name:'home'})
},
},
}
</script>
const routes = [
{
path : '/user/:userId',
name:'User',
component:User
}
]
pattern | mathced path | $route.params |
---|---|---|
/user/:userName | /user/join | { username: 'john'} |
/user/:userName/article/:articleId | /user/john/article/12 | {username: 'john', articleId:12} |
// index.js
const routes = [
{
path: '/lotto/:lottoNum',
name: 'lotto',
component: () => import(/* webpackChunkName: "about" */ '../views/TheLotto.vue')
},
]
//TheLotto.vue
<template>
<div>
<h1>로또</h1>
<h2>{{ $route.params.lottoNum }} 개의 번호를 추첨합니다</h2>
<button @click="someFunc">Get Lucky Numbers</button>
<p>{{ selectedLuckyNums }}</p>
</div>
</template>
<script>
import _ from "lodash"
export default {
name :'TheLotto',
data : function() {
return {
selectedLuckyNums: [],
}
},
methods :{
// 프로그래밍방식네비게이션
someFunc :function() {
const numbers = _.range(1,46)
this.selectedLuckyNums = _.sampleSize(numbers,this.$route.params.lottoNum)
},
},
</script>
//App.vue
<template>
<div id="app">
<nav>
<router-link :to="{name:'lotto', params: {lottoNum:6}}">Lotto</router-link> |
</nav>
<router-view/>
</div>
</template>