[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - Vue Router

2022. 3. 7. 23:29ใ†Front-End ์ž‘์—…์‹ค/Vue.js

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ—‚ ๋ชฉ์ฐจ

[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - ๊ธฐ๋ณธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ

[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - Vue Router

[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - Vue LifeCycle Hook

[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - data(), Data ์ฒ˜๋ฆฌ

[Vue.js] ๊ธฐ์ดˆ ๊ฐœ๋… ์ •๋ฆฌ - Watch, Array, for๋ฌธ(๋ฐ˜๋ณต๋ฌธ), if๋ฌธ

 

 

 

๐Ÿฑ Git Hub ์ฃผ์†Œ : https://github.com/junyharang-FrontEnd-coding-study/vue.js-study

๐Ÿ“– ์ฐธ๊ณ  ์ž๋ฃŒ : https://www.youtube.com/watch?v=sqH0u8wN4Rs

 

 

 

๐Ÿš€ VueRouter ๋ž€?

 

Vue ์—์„œ Routing ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์› ํ•ด ์ฃผ๋Š” ๊ณต์‹ Library ์ธ ๊ฒƒ์ด์—์š”.

์—ฌ๊ธฐ์„œ Routing์ด๋ผ๋Š” ๊ฒƒ์€ Web Paging ๊ฐ„์— ์ด๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•˜๋Š” ๊ฒƒ์ด์—์š”.
๊ธฐ์กด์—๋Š” ์ด์šฉ์ž๊ฐ€ Menu๋ฅผ Click ํ•˜๊ฑฐ๋‚˜, ํŠน์ • Button์„ Clickํ•˜๊ฒŒ ๋˜๋ฉด ์šฐ๋ฆฌ์˜ Web Site ์•ˆ์—์„œ ํŠน์ • Page๋กœ ์ด๋™ํ•  ๋•Œ, Server์— ์š”์ฒญ์„ ํ•˜๊ณ , ์ƒˆ๋กœ์šด Page๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ˜•์‹์œผ๋กœ ์ด๋ค„์ง€๊ฒŒ ๋˜์—ˆ์—ˆ๋˜ ๊ฒƒ์ด์—์š”.

Router๋ผ๋Š” ๊ฐœ๋…์€ Vue.js ๊ธฐ๋ฐ˜ ์›น์€ SPA(Sing Page Application)์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ผ Page์ž–์•„์š”?
๋ฏธ๋ฆฌ ํ•ด๋‹นํ•˜๋Š” Component Page๋ฅผ ๋‹ค ๋ฐ›์•„ ๋†“๊ณ , Routing์„ ์ด์šฉํ•ด์„œ ์ƒˆ๋กญ๊ฒŒ ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•˜๋Š” ๊ฒƒ์ด์—์š”.

Vue ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, React, Angular๋„ ๋™์ผํ•˜๊ฒŒ Routing์„ ์ด์šฉํ•ด์„œ ํ™”๋ฉด์„ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

ํ•ด๋‹น Project Package์—์„œ ์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ ํ•ด ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

๐Ÿš€ Layout

 

์ด์ œ src/components/ ์ด ๊ณณ์— layout์ด๋ผ๋Š” Directory๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค ๊ฒƒ์ด์—์š”.

๋ณดํ†ต Web site๋ฅผ ๋ณด๋ฉด ์ƒ๋‹จ์— Menu Bar๊ฐ€ ์žˆ๊ณ , ํ•˜๋‹จ์— Footer, ๊ฐ€์šด๋ฐ์— Contents ๋“ฑ์ด ์žˆ๋Š”๋ฐ, ์ƒ๋‹จ์— Menu๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๊ฐ€์šด๋ฐ Contents๊ฐ€ ๋ฐ”๋€Œ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ์„ ํ•˜๋Š”๋ฐ, ์‚ฌ์‹ค ์ƒ๋‹จ์— Menu๋Š” ๊ณ ์ •์ด๊ณ , ๋‹ค์‹œ Page๋ฅผ ๊ฐฑ์‹ ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ๋“ค์€ layout Directory์— ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

 

๋จผ์ € ์ƒ๋‹จ์„ ๋‚˜ํƒ€๋‚ด๋Š” `Header.vue`๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

 

 

Menu Bar๋Š” Bootstrap์„ ์ด์šฉํ•  ๊ฒƒ์ธ๋ฐ, Vue ์ „์šฉ์œผ๋กœ๋‹ค๊ฐ€ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

 

https://bootstrap-vue.org/

 

์—ฌ๊ธฐ `Get started`๋ฅผ ๋ˆŒ๋Ÿฌ ์ฃผ์„ธ์š”!

 

 

์œ„ ์‚ฌ์ง„์—์„œ with npm ๋ฐฉ์‹์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ bootstrap-vue๋ฅผ ์„ค์น˜ํ•  ๊ฒƒ์ด์—์š”.

 

 

์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Bootstrap๊ณผ Bootstrap Vue๊ฐ€ Module ํ˜•ํƒœ๋กœ `node_modules` Package์— Download๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

์œ„์™€ ๊ฐ™์ด ๋‘ ๊ฐœ๊ฐ€ ์ž˜ ๋ฐ›์•„์กŒ๋„ค์š”!

 

ํ•˜์ง€๋งŒ, ์ €๋ ‡๊ฒŒ ๋ฐ›์•„๋†จ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋Š” ์•„๋‹Œ ๊ฒƒ์ด์—์š”. ๋ฐ”๋กœ ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒƒ์ด์ง€์š”. import!

main.js์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ ํ•ด ์ค„ ๊ฒƒ์ด์—์š”.

 

 

์ € ๋ถ€๋ถ„์—์„œ ์ฃผ๋‹ˆํ•˜๋ž‘์€ ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ ํ•  ๊ฒƒ์ด์—์š”.

 

main.js

 

์œ„์™€ ๊ฐ™์ด Bootstrap๊ณผ ํ•จ๊ป˜ Design๋„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‹ˆ CSS ๋‚ด์šฉ๋„ ์ถ”๊ฐ€ ํ•ด ์ค„ ๊ฒƒ์ด์—์š”.

 

์ด์ œ Layout Header์— Menu Bar๋ฅผ ๋„ฃ์–ด์ค„ ๊ฒƒ์ด์—์š”.

 

Compponets / Navbar

 

์œ„์™€ ๊ฐ™์ด ์ด๋™์„ ํ•ด ์ค€ ๋‹ค์Œ ์•„๋ž˜ html ๋‚ด์šฉ์„ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•  ๊ฒƒ์ด์—์š”.

 

๊ทธ ์ „์—! Vue๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ `<template>` ์œผ๋กœ ๊ฐ์‹ธ์ง„ ๋ถ€๋ถ„๊ณผ `<script>`๋กœ ๊ฐ์‹ธ์ง„ ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ,
`<template>`์—๋Š” html Code๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„
`<script>`๋Š” html์˜ Data๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๊ฑฐ๋‚˜, Server์™€ ํ†ต์‹  ๋“ฑ์„ ํ•˜๊ธฐ ์œ„ํ•œ JavaScript Code๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„

์œ„์˜ Html Code๋Š” ๋‹น์—ฐํžˆ `<template>`์•ˆ์— ๋„ฃ์–ด ์ฃผ๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

 

๊ทธ๋Ÿฐ ๋‹ค์Œ `<script>` ์•ˆ์— `export default`์•ˆ์—๋Š” ์ด Component๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์ง€์ •ํ•  ๊ฒƒ์ธ๋ฐ, ์ด๋ฆ„์„ header๋ผ๊ณ  ์ง€์ •ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

๊ทธ ๋‹ค์Œ์—” `views`๋ผ๋Š” Directory๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‹ค์ œ Page๋ฅผ ์ด ๊ณณ์— ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

 

 

๐Ÿš€ App.vue

์ด ๋ถ€๋ถ„์ด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜์ธ ๊ฒƒ์ด์—์š”.
Vue cli๋ฅผ ํ†ตํ•ด ์„ค์น˜๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์ด ๋“ค์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

์œ„์˜ App.vue๊ฐ€ ์•„๋ž˜ ์ฒ˜๋Ÿผ Browser์— ์ถœ๋ ฅ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

์ด์ œ ์•„๊นŒ ๋งŒ๋“  layout/Header.vue๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธฐ์กด์— ์žˆ๋˜ Code๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•  ๊ฒƒ์ด์—์š”.

 

App.vue

๋ฐ˜์‘ํ˜•

 

importํ•œ Header๋ฅผ ./(ํ˜„์žฌ Dircetory) ๋ฐ‘์— compponets ๋ฐ‘์— layout ๋ฐ‘์— Header.vue์—์„œ ๊ฐ€์ ธ์˜ค๊ณ , ์ด๋ฅผ 13๋ฒˆ์งธ Line์—์„œ Components๋กœ ๋“ฑ๋ก ํ•ด ์ฃผ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ ์ธ ๊ฒƒ์ด์—์š”.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด html์„ ์ž‘์„ฑํ•˜๋Š” `<template>` ์•ˆ์—์„œ ํ•ด๋‹น Components๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

App.vue์˜ &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;template&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

 

 

Header ๋ฐ‘์—๋Š” Contents๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„์œผ๋กœ ํ™”๋ฉด์ด ๊ฐฑ์‹ ๋˜๋Š” ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— router๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.
์ฆ‰ ํ™”๋ฉด์ด ๋ฐ”๋€Œ๋Š” ๋ถ€๋ถ„์„ `<router-view></router-view>` ์ด ๋ถ€๋ถ„์„ ํ†ตํ•ด Re Loading ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

views/Home.vue ์ƒ์„ฑ

 

์ด์ œ ํ™”๋ฉด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด views ๋ฐ‘์— Home.vue๋ฅผ ๋งŒ๋“ค์–ด ์ค€ ๊ฒƒ์ด์—์š”.

 

Home.vue

Home์—๋Š” ๋จผ์ € h1 Tag๋ฅผ ํ†ตํ•ด ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑ ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

๊ทธ๋Ÿฐ ๋‹ค์Œ About.vue๋„ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

 

Aoubt.vue ์ƒ์„ฑ

 

About.vue

 

๊ทธ๋ฆฌ๊ณ  ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑ์„ ํ•ด ์ฃผ๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

 

์ฆ‰, ์œ„์— App.vue์—์„œ  `<router-view></router-view>` ๋ถ€๋ถ„์ด ์ƒํ™ฉ์— ๋”ฐ๋ผ Home.vue๊ฐ€ ๋‚˜์˜ค๊ฑฐ๋‚˜, About.vue๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ ‡๊ฒŒ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

์•„์ง์€ Router์— ๋Œ€ํ•œ ์„ค์ •์ด ๋œ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜ ์ฒ˜๋Ÿผ /src ๋ฐ‘์— router.js๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด์—์š”.

router.js ์ƒ์„ฑ

 

router.js

 

์œ„์™€ ๊ฐ™์ด vue์™€ vue-router๋ฅผ import ํ•ด์ฃผ๊ณ , Home, About Components๋„ import ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

router.js

 

์œ„์™€ ๊ฐ™์ด router๋ฅผ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด ์‹ค์ œ Path๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ Component๊ฐ€ ๋งŒ๋“ค์–ด ์ ธ์„œ App.vue์˜ `<router-view></router-view>` ์ด ๊ณณ์— ํ•ด๋‹น Component๋ฅผ ๋ถˆ์–ด์™€์„œ Loading ์‹œํ‚ค๋Š” ๊ฐœ๋…์ธ ๊ฒƒ์ด์—์š”.

์ฆ‰, ์ „์ฒด Page๋Š” ๊ฐ€๋งŒํžˆ ์žˆ๋Š” ์ƒํƒœ์—์„œ `<router-view></router-view>` ์ด ๋ถ€๋ถ„๋งŒ Page Component๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฐ”๋€Œ๋Š” ๊ฐœ๋…์ธ ๊ฒƒ์ด์—์š”.

๋ณธ๊ฒฉ์ ์œผ๋กœ route๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ main.js์—์„œ ์„ ์–ธ์„ ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

main.js

 

์œ„์™€ ๊ฐ™์ด 3๋ฒˆ์งธ ์ค„ ์ฒ˜๋Ÿผ import๋ฅผ ํ•ด ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋ฆฌ๊ณ , new Vue()์—์„œ 18๋ฒˆ์งธ ์ค„์ฒ˜๋Ÿผ router๋ฅผ ์ •์˜ ํ•ด ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

์ฆ‰, App์„ Mount ํ•  ๋•Œ, router๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€ ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค.

 

# npm run serve

 

์œ„์˜ ๋ช…๋ น์–ด๋กœ Web์„ ๊ธฐ๋™ ์‹œ์ผœ ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

 

๐Ÿš€ VueRouter ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

Vue Router๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

1. Query
2. Parmas

 

๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹๊ณผ ์„ ์–ธ์  ๋ฐฉ์‹์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด์—์š”.

โ— Query
| {name : 'Query', query: {name: 'Full Stack ์ฃผ๋‹ˆํ•˜๋ž‘', age: 0 }}

โ—Parmas
| {name : 'Params', Params: {name: 'Full Stack ์ฃผ๋‹ˆํ•˜๋ž‘ 1', age: 1 }}

 

์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ Data๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š”๋ฐ, name์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ Router ํŒŒ์ผ ์•ˆ์— ์ž‘์„ฑ๋œ name๊ณผ ๋™์ผํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

์˜ˆ์ œ ์ฝ”๋“œ

import Vue from 'vue';
import VueRouter from 'vue-router';
import Main from '@/views/Main.vue';
import Query from '../views/Query.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Main',
    component: Main
  },
  {
    path: '/Query',
    name: 'Query',
    component: Query,
  },
  {
    path: '/Params',
    name: 'Params',
    // 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/Params.vue'),
    // true๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ props๋กœ๋„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
    props: true,
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;

 

Params๋กœ ๋ฐ์ดํ„ฐ๋กœ๋ฅด ์ „๋‹ฌ ํ•  ๋•Œ, router Option์—์„œ props: true๋กœ ์„ค์ •ํ•˜๋ฉด props๋กœ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

<template>
  <div>
    Main
    <div>
      <router-link to="/">Main ํŽ˜์ด์ง€๋กœ</router-link>
    </div>

    <div>
      <br />

      <div>
        <button @click="clickList">Query ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹</button>
      </div>
      <br />
      <div>
        <router-link
          :to="{ name: 'Query', query: { name: 'Query ์„ ์–ธ์  ๋ฐฉ์‹', age: 1 } }"
        >
          Query ์„ ์–ธ์  ๋ฐฉ์‹
        </router-link>
      </div>

      <br />

      <div>
        <button @click="clickParams">Params ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹</button>
      </div>

      <br />

      <div>
        <router-link
          :to="{
            name: 'Params',
            params: { name: 'Params ์„ ์–ธ์  ๋ฐฉ์‹', age: 1 },
          }"
        >
          Params ์„ ์–ธ์  ๋ฐฉ์‹
        </router-link>
      </div>
    </div>

    <router-view />
  </div>
</template>
<script>
export default {
  name: "App",
  methods: {
    clickList() {
      this.$router.push({
        name: "Query",
        query: { name: "Query ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹", age: 2 },
      });
    },
    clickParams() {
      this.$router.push({
        name: "Params",
        params: { name: "Params ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹", age: 2 },
      });
    },
  },
};
</script>

 

 

 

 

๐Ÿš€ VueRouter ์ฃผ์†Œ(URI)๋กœ ์ด๋™

Router ์ •๋ณด๊ฐ€ ์„ค์ • ๋˜์—ˆ๋‹ค๋ฉด ๊ฐ๊ฐ ํ•ด๋‹น๋œ Page, Router๋กœ ์ด๋™ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ์ด ๋•Œ, ์ด๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ moveTo() ๋“ฑ์„ ๋งŒ๋“ค์–ด `$router.push()` ๋ฐฉ๋ฒ•์œผ๋กœ ์ด๋™ ํ•˜๊ฑฐ๋‚˜, ์•„๋‹ˆ๋ฉด `<router-link>` Component๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

router-link Component๋Š” ํ•ด๋‹น Elment๋ฅผ ํด๋ฆญํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์œผ๋กœ ํ•ด๋‹น Route ์ทจ์น˜๋กœ ์ด๋™ํ•˜๋„๋ก Link๋ฅผ ์ƒ์„ฑ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”. ๋งŒ์•ฝ Home๊ณผ SiteList๋กœ ์ด๋™ํ•˜๋Š” Link๋ฅผ ๊ฐ๊ฐ ๋งŒ๋“ ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

<router-link :to="/">Home<router-link>
<router-link :to="/siteList">SiteList<router-link>
728x90
๋ฐ˜์‘ํ˜•