ํ์ด์ง๋ฅผ ์ฌ๋ฌ๊ฐ ๋ง๋ค๊ณ ์ถ์ ๋ ๋ผ์ฐํฐ๋ฅผ ์ฌ์ฉํ๋ค.
npm install vue-router@4
import { createWebHistory, createRouter } from "vue-router";
const routes = [
{
path: "/๊ฒฝ๋ก",
component: importํด์จ ์ปดํฌ๋ํธ,
}
];
const router = createRouter({
history: createWebHistory(),
routes,
});
export default router;
import router from './router'
createApp(App).use(router).mount('#app')
ex) App.vue์ HTML ๋ด๋ถ ์๋ฌด ๊ณณ์๋ ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ฉด ๋ผ์ฐํฐ๋ก ๊ตฌ๋ถ๋ ํ์ด์ง๋ฅผ ๊ทธ ์๋ฆฌ์ ๋ณด์ฌ์ค๋ค.
<router-view></router-view>
to=""
์์ ๋ง์๋๋ก ๊ฒฝ๋ก ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
<router-link to="/list">์ด๋ํ๊ธฐ</router-link>
router.js
const routes = [
{
path : '/detail/:id,
component : Detail,
}
]
/detail/
๋ค์ ์๋ฌด๊ฑฐ๋ ์์ฑํ์ ๋ ํญ์ Detail.vue๋ฅผ ๋ณด์ฌ์ฃผ๋ผ๋ ๋ง์ด๋ค.
:
์ฝ๋ก ๊ธฐํธ๋ฅผ ๋ถ์ด๊ณ ๋ค์ ์๋ฌด๊ฑฐ๋ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
:
๋ค์ ์๋ URL์ ๊ฐ์ ธ์ค๊ธฐ{{ $route.params.ํ๋ผ๋ฏธํฐ๋ช
}}
ํน์ ํ์ด์ง ๋ด์์ ๋ผ์ฐํธ๋ฅผ ๋๋๋ ๊ฒฝ์ฐ๋ฅผ nested routes๋ผ๊ณ ํ๋ค.
const routes = [
{
path : '/fruit/:id',
component : Fruit,
children : [
{ path : 'banana', component : Banana},
{ path : 'apple', component : Apple },
]
}
]
Fruit.vue์ <router-view></route-view>
๋ผ๋ ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ฉด /fruit/:id/banana
๋ก ์ ์ํ์ ๋ <router-view>
์๋ฆฌ์ Banana ์ปดํฌ๋ํธ๊ฐ ๋ณด์ธ๋ค.
$router.push('/๊ฒฝ๋ก')
์ค์ ํ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ค.
$router.go(-1)
๋ค๋ก๊ฐ๊ธฐ ๊ธฐ๋ฅ์ ์คํํ ์ ์๋ค.
1
๋ผ๊ณ ์์ฑํ๋ฉด ์์ผ๋ก ์คํ ํ ์ ์๋ค.
$router ๋ณ์๋ฅผ ์ ์ฌ์ฉํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ฐฉ๋ฌธ๊ธฐ๋ก์ ๋ค์ถฐ๋ณด๊ฑฐ๋ ๋ง์๋๋ก ์กฐ์ํ ์ ์๋ค.