Vue Router
URL์ ๋ฐ๋ฅธ ๋ค๋ฅธ component rendering
Router ๊ฐ ํ์ํ ์ด์
SPA ์ ํน์ฑ์ ์ด๋ฆฌ๋, URL์ ๋ฌ๋ฆฌ ํ๊ธฐ ์ํด
URL์ ๋ฐ๋ฅธ ๋ค๋ฅธ component rendering
Route/view mapping
Vue ์ ํน์ง
์๊ณ ๊ฐ๋ณ์ง๋ง, ํ์ํ ๊ธฐ๋ฅ์ ์ถ๊ฐ ํ ์ ์๊ฒ ํ์!
๊ทธ๋์ router ์ฌ์ฉํ๋ ค๋ฉด ๋ฐ๋ก ์ถ๊ฐํด์ผํจ!
Install Vue Router
์ผ๋ฐ ์ค์น
npm install vue-router
vue์ ํ์ ๋น๋ ค add ํ๊ธฐ
Project ์ต์๋จ์์ ์คํ!
$ vue add router
WARN There are uncommited changes in the current repository, it's recommended to commit or stash them first.
? Still proceed? Yes
๐ฆ Installing @vue/cli-plugin-router...
+ @vue/cli-plugin-router@4.4.1
added 2 packages from 1 contributor, updated 1 package and audited 1283 packages in 5.023s
46 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
โ Successfully installed plugin: @vue/cli-plugin-router
? Use history mode for router? (Requires proper server setup for index fall
back in production) Yes
๐ Invoking generator for @vue/cli-plugin-router...
โ Running completion hooks...
โ Successfully invoked generator for plugin: @vue/cli-plugin-router
๊ฐ๋ฅํ project ์์ฑํ์ ๋ง์ add ํ๊ธฐ!
์ด๋ฐ ๋ฉ์์ง ๋จ์ง ์๊ฒ ํด๋ผ~
ex)
App.vue
App.vue
<template>
<div id="app">
<div id="nav">
<!-- ์ด๋ ๊ฒ a tag ์ฐ๋ฉด page reload ๋จ -->
<a href="/">Home</a>
<a href="/about">About</a>
<br>
<!-- ๊ทธ๋์ router-link ๋ฅผ ์จ์ผ ํจ! page reload ๋๋ฉด ์ฐ๋ฆฌ๊ฐ vue๋ฅผ ์ธ ์ด์ ๊ฐ ์์! -->
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
router > index.js
index.js
router ์์ ์๋
index.js
๋ Django ์์urls.py
๊ฐ์ ๊ฒ!
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
Vue.use(VueRouter)
// Django ์์ urls.py ๊ฐ์ ๊ฒ!
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
views/
directory
views/
directoryURL(/) ๊ณผ mapping ๋ component๋ค์ด ๋ค์ด๊ฐ๋ directory
URL์ ํ์ฌ ๋ฐ์ ์ ๋ค~!
views/
์ ์๋ Component๋ค์, router/index.js ๋ก ๊ฐ์ import ํ๋ค!
variable routing
<template>
<div>
<h1>Hello, {{ name }}</h1>
</div>
</template>
<script>
export default {
name: 'HelloName',
data: function() {
return {
name: this.$route.params.name,
}
}
}
</script>
Ping - Pong ๋ง๋ค๊ธฐ
Ping.vue
Ping.vue
<template>
<div>
<h1>Ping</h1>
<input type="text" @keyup.enter="sendToPong" v-model="inputText"/>
</div>
</template>
<script>
export default {
name: 'Ping',
data() {
return {
inputText: '',
}
},
methods: {
sendToPong: function() {
// $router
// ver1)
// this.$router.push(`pong?message=${this.inputText}`)
// ver2)
this.$router.push({ name: 'Pong', query: {message: this.inputText}})
}
}
}
</script>
<style>
</style>
Pong.vue
Pong.vue
<template>
<div>
<h1>Pong</h1>
<h3>{{messageFromPing}}</h3>
</div>
</template>
<script>
export default {
name: 'Pong',
data(){
return {
messageFromPing: this.$route.query.message
}
}
}
</script>
<style>
</style>
Last updated
Was this helpful?