Skip to main content

경로 세그먼트의 수를 알 수 없다면 자바스크립트의 나머지 매개변수와 비슷한 형태인 [...rest] 매개변수를 사용할 수 있습니다.

src/routes/[path]src/routes/[...path]로 변경하세요. 이렇게 하면 해당 라우트가 모든 경로와 일치하게 됩니다.

다른 경로들이 먼저 테스트되고, 그 경로들에 일치하지 않을 경우에 이 라우트가 사용됩니다. 이는 나머지 매개변수가 모든 경우를 처리하여 'catch-all' 역할을 한다는 뜻입니다. 예를 들어, /categories/...내의 페이지에 사용자 정의 404 페이지가 필요한 경우 다음과 같은 파일을 추가할 수 있습니다.

src/routes/
├ categories/
│ ├ animal/
│ ├ mineral/
│ ├ vegetable/
│ ├ [...catchall]/
│ │ ├ +error.svelte
│ │ └ +page.server.js

+page.server.js 파일의 load 함수 안에 throw error(404)를 추가하세요.

나머지 파라미터가 끝에 위치할 필요는 없습니다. 예를 들어 /items/[...path]/edit 또는 /items/[...path].json 과 같이 위치할 수도 있습니다.

Next: Param matchers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script>
	import { page } from '$app/stores';
 
	let words = ['how', 'deep', 'does', 'the', 'rabbit', 'hole', 'go'];
 
	$: depth = $page.params.path.split('/').filter(Boolean).length;
	$: next = depth === words.length ? '/' : `/${words.slice(0, depth + 1).join('/')}`;
</script>
 
<div class="flex">
	{#each words.slice(0, depth) as word}
		<p>{word}</p>
	{/each}
 
	<p><a href={next}>{words[depth] ?? '?'}</a></p>
</div>
 
<style>
	.flex {
		display: flex;
		height: 100%;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
 
	p {
		margin: 0.5rem 0;
		line-height: 1;
	}
 
	a {
		display: flex;
		width: 100%;
		height: 100%;
		align-items: center;
		justify-content: center;
		font-size: 4rem;
	}
</style>
 
initialising