Basic Svelte
Introduction
Bindings
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
Different routes of your app will often share common UI. Instead of repeating it in each +page.svelte
component, we can use a +layout.svelte
component that applies to all routes in the same directory.
In this app we have two routes, src/routes/+page.svelte
and src/routes/about/+page.svelte
, that contain the same navigation UI. Let’s create a new file, src/routes/+layout.svelte
...
src/routes/
├ about/
│ └ +page.svelte
├ +layout.svelte
└ +page.svelte
...and move the duplicated content from the +page.svelte
files into the new +layout.svelte
file. The {@render children()}
tag is where the page content will be rendered:
src/routes/+layout
<script>
let { children } = $props();
</script>
<nav>
<a href="/">home</a>
<a href="/about">about</a>
</nav>
{@render children()}
<script lang="ts">
let { children } = $props();
</script>
<nav>
<a href="/">home</a>
<a href="/about">about</a>
</nav>
{@render children()}
A +layout.svelte
file applies to every child route, including the sibling +page.svelte
(if it exists). You can nest layouts to arbitrary depth.
previous next
1
2
3
4
5
6
7
8
<nav>
<a href="/">home</a>
<a href="/about">about</a>
</nav>
<h1>home</h1>
<p>this is the home page.</p>