Skip to content

Commit 47b13ce

Browse files
author
Rich Harris
committed
navigating
1 parent de31159 commit 47b13ce

File tree

7 files changed

+79
-1
lines changed

7 files changed

+79
-1
lines changed

content/tutorial/03-sveltekit/07-stores/02-navigating-store/README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,34 @@
22
title: navigating
33
---
44

5-
> Coming soon
5+
The `navigating` store represents the current navigation. When a navigation starts — because of a link click, or a back/forward navigation, or a programmatic `goto` — the value of `navigation` will become an object with the following properties:
6+
7+
- `from` and `to` — objects with `params`, `route` and `url` properties
8+
- `type` — the type of navigation, e.g. `link`, `popstate` or `goto`
9+
10+
> For complete type information visit the [`Navigation`](https://kit.svelte.dev/docs/types#public-types-navigation) documentation.
11+
12+
It can be used to show a loading indicator for long-running navigations. In this exercise, `src/routes/+page.server.js` and `src/routes/about/+page.server.js` both have an artificial delay. Inside `src/routes/+layout.svelte`, import the `navigating` store and add a message to the nav bar:
13+
14+
```svelte
15+
/// file: src/routes/+layout.svelte
16+
<script>
17+
import { page, +++navigating+++ } from '$app/stores';
18+
</script>
19+
20+
<nav>
21+
<a href="/" aria-current={$page.url.pathname === '/'}>
22+
home
23+
</a>
24+
25+
<a href="/about" aria-current={$page.url.pathname === '/about'}>
26+
about
27+
</a>
28+
29+
+++ {#if $navigating}
30+
navigating to {$navigating.to.url.pathname}
31+
{/if}+++
32+
</nav>
33+
34+
<slot />
35+
```
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 />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function load() {
2+
return new Promise((fulfil) => {
3+
setTimeout(fulfil, 1000);
4+
});
5+
}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function load() {
2+
return new Promise((fulfil) => {
3+
setTimeout(fulfil, 1000);
4+
});
5+
}
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
import { page, navigating } 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+
14+
{#if $navigating}
15+
navigating to {$navigating.to.url.pathname}
16+
{/if}
17+
</nav>
18+
19+
<slot />

0 commit comments

Comments
 (0)