[Vue3] Vue Router2

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

๐Ÿ’ป Vue Router2

๋™์  ๋ผ์šฐํŠธ

์ฃผ์–ด์ง„ ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ผ์šฐํŠธ๋ฅผ ๋™์ผํ•œ ์ปดํฌ๋„ŒํŠธ์— ๋งคํ•‘ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ž์ฃผ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž ๋ชฉ๋ก(User List) ๋Š” /users์™€ ๊ฐ™์€ ๊ฒฝ๋กœ์— ๋งคํ•‘๋˜๋ฉด ๋˜์ง€๋งŒ ์‚ฌ์šฉ์ž ์ƒ์„ธ(User Detail) ๋Š” ์‚ฌ์šฉ์ž ์‹๋ณ„์ž ๋ณ„๋กœ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ์— ๋งคํ•‘ ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿด๋•Œ Vue Router์—์„œ๋Š” ๊ฒฝ๋กœ์—์„œ ๋™์  ์„ธ๊ทธ๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ param๋ผ๊ณ  ํ•œ๋‹ค.

ex) /user/alice, /user/alice2, /user/alice3 .... -> UserComponent.vue

index.js

// router/index.js

	// ':' <- ๋™์  ๋ผ์šฐํŒ…์ด๋ผ๋„ํ•˜๋ฉฐ ์—ฌ๋Ÿฌ ๊ฐœ์˜ URL์„ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ์— ๋งตํ•‘ํ•˜๊ณ  ์‹ถ์„๋•Œ
	// ํ‘œํ˜„ํ•˜๋Š” ํ‘œํ˜„ ๋ฐฉ์‹์ด๋‹ค.
	// ์ด ์„ธ๋ฏธ์ฝœ๋ก ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ ํ•ด์„œ ๋ผ์šฐํŠธ ๊ฐ์ฒด์— params๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. -> {{ $route.params }}
	{
		path: '/posts/:id',
		name: 'PostDetail',
		component: PostDetailView,
	},
	{
		path: '/posts/:id/edit',
		name: 'PostEdit',
		component: PostEditView,
	},
  • ๋™์  ์„ธ๊ทธ๋จผํŠธ๋Š” ์ฝœ๋ก (:)์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์  ์„ธ๊ทธ๋จผํŠธ์˜ ๊ฐ’์€ $route.params ํ•„๋“œ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

<template>
	<div>
		<h2>์ œ๋ชฉ</h2>
		<p>{{ $route.params }}</p>
		.....
</template>

์ด๋ ‡๊ฒŒ $route.params ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.


1-1. query, hash

$route.params์™ธ์—๋„ $route ๊ฐ์ฒด๋Š” $route.query(์ฟผ๋ฆฌ์ŠคํŠธ๋ง), $route.hash(ํ•ด์‹œํƒœ๊ทธ) ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์œ ์šฉํ•œ ์ •๋ณด๋„ ๋…ธ์ถœํ•œ๋‹ค.

query

<template>
	<div>
		<h2>์ œ๋ชฉ</h2>
		<p>params: {{ $route.params }}</p>
		<p>query: {{ $route.query }}</p>
		<p>query value: {{ $route.query.searchText }}</p>
      .....

hash

<template>
	<div>
		<h2>์ œ๋ชฉ</h2>
		<p>params: {{ $route.params }}</p>
		<p>query: {{ $route.query }}</p>
		<p>query value: {{ $route.query.searchText }}</p>
		<p>hash: {{ $route.hash }}</p>
      .....


ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ๋„ค๋น„๊ฒŒ์ด์…˜

<RouterLink>๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ์  ๋„ค๋น„๊ฒŒ์ด์…˜์šฉ anchor ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ๋ผ์šฐํ„ฐ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

router.push

๋‹ค๋ฅธ URL๋กœ ์ด๋™ํ•˜๋ ค๋ฉด router.push๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ๋กœ์šด ํ•ญ๋ชฉ์„ ํžˆ์Šคํ† ๋ฆฌ์Šคํƒ์— ๋„ฃ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์˜ ๋’ค๋กœ ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ด์ „ URL๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” <RouterLink>๋ฅผ ํด๋ฆญ ํ•  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋ฏ€๋กœ <RouterLink :to="..">๋ฅผ ํด๋ฆญํ•˜๋ฉด router.push(..)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.


404 Not Found Route

์ผ๋ฐ˜ ํŒŒ๋ผ๋ฏธํ„ฐ(:id)๋Š” ์Šฌ๋ž˜์‰ฌ(/)๋กœ ๊ตฌ๋ถ„๋œ URL ์‚ฌ์ด์˜ ๋ฌธ์ž๋งŒ ์ผ์น˜ ์‹œํ‚จ๋‹ค. ๋ฌด์—‡์ด๋“  ์ผ์น˜์‹œํ‚ค๋ ค๋ฉด param๋ฐ”๋กœ ๋’ค์— ๊ด„ํ˜ธ ์•ˆ์— ์ •๊ทœ์‹(regexp)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

//404 Not found
	{
		path: '/:pathMatch(.*)*',
		name: 'NotFound',
		component: NotFoundView,
	},

์ค‘์ฒฉ ๋ผ์šฐํŠธ(Nested Routes)

์‹ค์ œ ์•ฑ UI๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋กœ ์ค‘์ฒฉ ๋œ ์ปดํฌ๋„ŒํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. URL์˜ ์„ธ๊ทธ๋จผํŠธ๊ฐ€ ์ค‘์ฒฉ ๋œ ์ปดํฌ๋„ŒํŠธ์˜ ํŠน์ • ๊ตฌ์กฐ์™€ ์ผ์น˜ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ผ๋ฐ˜์ ์ด๋‹ค.

//์ค‘์ฒฉ Router
	{
		path: '/nested',
		name: 'NestedView',
		component: NestedView,
		children: [
			{
               //๊ธฐ๋ณธ nested๋ฅผ ํด๋ฆญ ํ–ˆ์„ ๋•Œ ์ฒ˜์Œ ๋ณด์—ฌ์ง€๋Š” ํŽ˜์ด์ง€ ์„ค์ •
				path: '',
				name: 'NestedHome',
				component: NestedHomeView,
			},
			{
				path: 'one', // /nested/one
				name: 'NestedOne',
				component: NestedOneView,
			},
			{
				path: 'two', // /nested/two
				name: 'NestedTwo',
				component: NestedTwoView,
			},
		],
	},
profile
์˜๋ฌธ์€ '์‚ถ์˜ ์ˆ˜์ค€'์„ ๊ฒฐ์ •ํ•˜๊ณ , ์งˆ๋ฌธ์€ '์‚ถ ์ž์ฒด'๋ฅผ ๋ฐ”๊พผ๋‹ค.

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