๋ฐ์ํ
โ๏ธ vue-router ๋?
๋ผ์ฐํ ์ ๊ฐ๋จํ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
vue-router๋ SPA๋ฅผ ๋ง๋๋ ๊ฒ์ ์ฉ์ดํ๊ฒ ํด์ค๋ค.
์ค์น ๋ฐฉ๋ฒ
ํ๋ก์ ํธ๊ฐ ์๋ ๊ฒฝ๋ก๋ก ๊ฐ์ terminal์ ์๋์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ค.
npm install vue-router
์ค์น๋ฅผ ํ๋ฉด package.json์ dependencies์ ์๋์ ๊ฐ์ ๋ฌธ๊ตฌ๊ฐ ์๋ก ์๊ธด๋ค.
router์ ์ฌ์ฉํ ๋์๋ src ํด๋ ์๋์ 'router'๋ผ๋ ํด๋๋ฅผ ์์ฑํ๊ณ
๊ทธ ์์ index.js๋ผ๋ ํ์ผ์ ๋ง๋ ๋ค.
์ด๋ ๊ฒ ๋ง๋ค์๊ณ ํ ๊ฒ์ ์ฝ์์ด๋ผ๊ณ ํ๋ค.
src/router/index.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../components/Home.vue'
import About from '../components/About.vue'
Vue.use(VueRouter);
const routes = [
{
path: "/home",
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;
src/main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router';
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')
src/App.vue
<template>
<div id="app">
<h1>Vue-router Test</h1>
<div id="nav">
<router-link to="/home">Home</router-link> ใ
ฃ
<router-link to="/about">About</router-link>
</div>
<router-view />
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #000;
}
#nav a.router-link-exact-active {
color: blue;
}
</style>
๋๋ต ์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ๋ค.
๋ฐ์ํ
'JavaScript > Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vue.js] v-bind ๋? (0) | 2021.12.28 |
---|---|
[Vue.js] ์ ์ธ ์์ (0) | 2021.10.25 |
[Vue.js] vue-router๋ฅผ ์ฌ์ฉํ๋ ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ (0) | 2021.10.25 |
@ : ์ ๋ ๊ฒฝ๋ก(root)๋ถํฐ ์์ (0) | 2021.10.22 |
Vue.js ๊ณต๋ถ 1์ผ์ฐจ (0) | 2021.10.18 |
๋๊ธ