Skip to content

Commit e41e120

Browse files
author
Rich Harris
committed
page store
1 parent fbb547c commit e41e120

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

content/tutorial/03-sveltekit/07-stores/01-page-store/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,35 @@
22
title: page
33
---
44

5-
> Coming soon
5+
As we learned [earlier](writable-stores), Svelte stores are a place to put data that doesn't belong to an individual component.
6+
7+
SvelteKit makes three readonly stores available via the `$app/stores` module — `page`, `navigating` and `updating`. The one you'll use most often is [`page`](https://kit.svelte.dev/docs/types#public-types-page), which provides information about the current page:
8+
9+
* `url` — the [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) of the current page
10+
* `params` — the current page's [parameters](params)
11+
* `route` — an object with an `id` property representing the current route
12+
* `status` — the HTTP status code of the current page
13+
* `error` — the error object of the current page, if any (you'll learn more about error handling in [later](error-basics) [exercises](handleerror))
14+
* `data` — the data for the current page, combining the return values of all `load` functions
15+
* `form` — the data returned from a [form action](the-form-element)
16+
17+
As with any other store, you can reference its value in a component by prefixing its name with the `$` symbol. For example, we can access the current pathname as `$page.url.pathname`:
18+
19+
```svelte
20+
/// file: src/routes/+layout.svelte
21+
+++<script>
22+
import { page } from '$app/stores';
23+
</script>+++
24+
25+
<nav>
26+
<a href="/" +++aria-current={$page.url.pathname === '/'}+++>
27+
home
28+
</a>
29+
30+
<a href="/about" +++aria-current={$page.url.pathname === '/about'}+++>
31+
about
32+
</a>
33+
</nav>
34+
35+
<slot />
36+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<nav>
2+
<a href="/">
3+
home
4+
</a>
5+
6+
<a href="/about">
7+
about
8+
</a>
9+
</nav>
10+
11+
<slot />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>home</h1>
2+
<p>this is the home page.</p>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>about</h1>
2+
<p>this is the about page.</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import { page } from '$app/stores';
3+
</script>
4+
5+
<nav>
6+
<a href="/" aria-current={$page.url.pathname === '/'}>
7+
home
8+
</a>
9+
10+
<a href="/about" aria-current={$page.url.pathname === '/about'}>
11+
about
12+
</a>
13+
</nav>
14+
15+
<slot />

content/tutorial/common/src/app.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@
156156
border-radius: var(--border-radius);
157157
}
158158

159+
nav a {
160+
text-decoration: none;
161+
}
162+
163+
nav a[aria-current="true"] {
164+
border-bottom: 2px solid;
165+
}
166+
159167
ul:has(form) {
160168
list-style: none;
161169
padding: 0;

0 commit comments

Comments
 (0)