[Vue3] Vue Router

chaewonยท2024๋…„ 8์›” 7์ผ
0
post-thumbnail

๐Ÿ’ปVue Router

๋ทฐ ๋ผ์šฐํ„ฐ๋Š” Vue.js๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(SPA)์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Vue.js์˜ ๊ณต์‹ ๋ผ์šฐํ„ฐ๋‹ค. Vue Router ๊ณต์‹๋ฌธ์„œ


๐Ÿ’ก ๋ผ์šฐํ„ฐ(Router)๋ž€?

๋ผ์šฐํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋„คํŠธ์›Œํฌ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ์žฅ์น˜๋ฅผ ๋งํ•œ๋‹ค. ๋ทฐ์—์„œ ๋งํ•˜๋Š” ๋ผ์šฐํ„ฐ๋Š” ์‰ฝ๊ฒŒ ๋งํ•ด์„œ URL์— ๋”ฐ๋ผ ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„์ง€ ๋งคํ•‘ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด " /home ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ Home.vue ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•ด๋ผ" ๋ผ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ /home -> Home.vue ์ด๋Ÿฌํ•œ ๋งคํ•‘์ •๋ณด๋ฅผ ๋ผ์šฐํŠธ(Route)๋ผ๊ณ ๋„ ํ•œ๋‹ค.

๋ผ์šฐํŠธ(Route)๋ž€?

  • ์–ด๋–ค URL์— ๋Œ€ํ•ด ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด

์„ค์น˜

npm install vue-router

1-1. ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ

HomeView.vue์™€ AboutView.vue ํŽ˜์ด์ง€(์ปดํฌ๋„ŒํŠธ)๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ฒฝ๋กœ๋Š” root์— ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ๋„ฃ์–ด์ค€๋‹ค src/views/

HomeView.vue

src/views/HomeView.vue

<template>
	<div>Home view</div>

</template>

<script setup>
</script>

<style lang="scss" scoped></style>

AboutView.vue

src/views/AboutView.vue

<template>
	<div>About View</div>
</template>

<script setup>
</script>

<style lang="scss" scoped></style>

1-2. ๋ผ์šฐํ„ฐ(router) ์„ค์ •

index.js

// src/router/index.js

//๋ผ์šฐํŠธ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ผ์šฐํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '@/views/HomeView.vue';
import AboutView from '@/views/AboutView.vue';

//router์„ค์ •
const routes = [
	{
		path: '/', //root๋กœ ๋“ค์–ด์™”์„ ๋•Œ
		name: 'Home',
		component: HomeView,
	},
	{
		path: '/about', //about๋กœ ๋“ค์–ด์™”์„ ๋•Œ
		name: 'About',
		component: AboutView,
	},
];

//๋ผ์šฐํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ
const router = createRouter({
	history: createWebHistory('/'),
	routes, // ํ‚ค์™€ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ฐ’์„ ๋นผ๊ณ  ์ ์–ด๋„ ๋จ
});

//์ƒ์„ ๋œ router ๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก export
export default router;

์„ค์ •ํ•œ ๋ผ์šฐํ„ฐ ๊ฐ์ฒด๋ฅผ Vue ์ธ์Šคํ„ด์Šค์— ์ถ”๊ฐ€ํ•ด๋ณด์ž

main.js

// src/main.js

//bootstrap ์„ค์น˜ ํ›„ import
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap-icons/font/bootstrap-icons.css';

import { createApp } from 'vue';
import App from './App.vue';

//router/index.js์—์„œ ์„ค์ •ํ•œ router ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ด
import router from '@/router';

//use ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์„ค์ •ํ•œ๋‹ค.
createApp(App).use(router).mount('#app');
import 'bootstrap/dist/js/bootstrap.js';

app.use(router)๋ฅผ ํ˜ธ์ถœ ํ•จ์œผ๋กœ์จ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ $router, $route ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.


Vue Router ์—์„œ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•  ๋•Œ๋Š” ์ผ๋ฐ˜ aํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ์ธ <RouterLink>๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํŽ˜์ด์ง€ ๋งํฌ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด Vue Router๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ฆฌ๋กœ๋”ฉ ํ•˜์ง€ ์•Š๊ณ  URL์— ๋งคํ•‘๋œ ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค.

RouterView

<RouterView>๋Š” URL์— ๋งคํ•‘๋œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•œ๋‹ค.


push, replace

router.push์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜์ง€๋งŒ ์œ ์ผํ•œ ์ฐจ์ด๋Š” ์ƒˆ๋กœ์šด ํžˆ์Šคํ† ๋ฆฌ ํ•ญ๋ชฉ์— ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ํ˜„์žฌ ํ•ญ๋ชฉ์„ ๋Œ€์ฒดํ•œ๋‹ค.

// router.push ๋ฉ”์†Œ๋“œ์— replace: true ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
router.push({ path: '/home', replace: true})
// equivalent to
router.replace({ path: '/home'})
profile
์˜๋ฌธ์€ '์‚ถ์˜ ์ˆ˜์ค€'์„ ๊ฒฐ์ •ํ•˜๊ณ , ์งˆ๋ฌธ์€ '์‚ถ ์ž์ฒด'๋ฅผ ๋ฐ”๊พผ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€