You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> In Svelte 4, you'd use `export let data` instead
55
+
51
56
> [!NOTE] Note that SvelteKit uses `<a>` elements to navigate between routes, rather than a framework-specific `<Link>` component.
52
57
53
58
### +page.js
@@ -153,21 +158,29 @@ But in many apps, there are elements that should be visible on _every_ page, suc
153
158
154
159
To create a layout that applies to every page, make a file called `src/routes/+layout.svelte`. The default layout (the one that SvelteKit uses if you don't bring your own) looks like this...
155
160
156
-
```html
157
-
<slot></slot>
161
+
```svelte
162
+
<script>
163
+
let { children } = $props();
164
+
</script>
165
+
166
+
{@render children()}
158
167
```
159
168
160
-
...but we can add whatever markup, styles and behaviour we want. The only requirement is that the component includes a `<slot>` for the page content. For example, let's add a nav bar:
169
+
...but we can add whatever markup, styles and behaviour we want. The only requirement is that the component includes a `@render` tag for the page content. For example, let's add a nav bar:
170
+
171
+
```svelte
172
+
<!---- file: src/routes/+layout.svelte --->
173
+
<script>
174
+
let { children } = $props();
175
+
</script>
161
176
162
-
```html
163
-
/// file: src/routes/+layout.svelte
164
177
<nav>
165
178
<a href="/">Home</a>
166
179
<a href="/about">About</a>
167
180
<a href="/settings">Settings</a>
168
181
</nav>
169
182
170
-
<slot></slot>
183
+
{@render children()}
171
184
```
172
185
173
186
If we create pages for `/`, `/about` and `/settings`...
@@ -196,8 +209,8 @@ We can create a layout that only applies to pages below `/settings` (while inher
Throughout the examples above, we've been importing types from a `$types.d.ts` file. This is a file SvelteKit creates for you in a hidden directory if you're using TypeScript (or JavaScript with JSDoc type annotations) to give you type safety when working with your root files.
372
385
373
-
For example, annotating `exportlet data` with `PageData` (or `LayoutData`, for a `+layout.svelte` file) tells TypeScript that the type of `data` is whatever was returned from `load`:
386
+
For example, annotating `let{ data } =$props()` with `PageData` (or `LayoutData`, for a `+layout.svelte` file) tells TypeScript that the type of `data` is whatever was returned from `load`:
> In Svelte 4, you'd use `export let data` instead
37
+
35
38
Thanks to the generated `$types` module, we get full type safety.
36
39
37
40
A `load` function in a `+page.js` file runs both on the server and in the browser (unless combined with `export const ssr = false`, in which case it will [only run in the browser](page-options#ssr)). If your `load` function should _always_ run on the server (because it uses private environment variables, for example, or accesses a database) then it would go in a `+page.server.js` instead.
@@ -85,13 +88,13 @@ export async function load() {
> In Svelte 4, you'd use `exportlet data` and `exportlet form` instead to declare properties
156
+
157
157
### Validation errors
158
158
159
159
If the request couldn't be processed because of invalid data, you can return validation errors — along with the previously submitted form values — back to the user so that they can try again. The `fail` function lets you return an HTTP status code (typically 400 or 422, in the case of validation errors) along with the data. The status code is available through `$page.status` and the data through `form`:
@@ -339,8 +339,8 @@ The easiest way to progressively enhance a form is to add the `use:enhance` acti
Copy file name to clipboardExpand all lines: documentation/docs/30-advanced/10-advanced-routing.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,7 +193,7 @@ You can also put a `+page` directly inside a `(group)`, for example if `/` shoul
193
193
194
194
### Breaking out of layouts
195
195
196
-
The root layout applies to every page of your app — if omitted, it defaults to `<slot />`. If you want some pages to have a different layout hierarchy than the rest, then you can put your entire app inside one or more groups _except_ the routes that should not inherit the common layouts.
196
+
The root layout applies to every page of your app — if omitted, it defaults to `{@render children()}`. If you want some pages to have a different layout hierarchy than the rest, then you can put your entire app inside one or more groups _except_ the routes that should not inherit the common layouts.
197
197
198
198
In the example above, the `/admin` route does not inherit either the `(app)` or `(marketing)` layouts.
199
199
@@ -260,11 +260,11 @@ Not all use cases are suited for layout grouping, nor should you feel compelled
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/26-$lib.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,4 +2,18 @@
2
2
title: $lib
3
3
---
4
4
5
-
TODO
5
+
SvelteKit automatically makes files under `src/lib` available using the `$lib` import alias. You can change which directory this alias points to in your [config file](configuration#files).
0 commit comments