File tree Expand file tree Collapse file tree 17 files changed +125
-140
lines changed
src/routes/tutorial/[slug] Expand file tree Collapse file tree 17 files changed +125
-140
lines changed Original file line number Diff line number Diff line change @@ -6,11 +6,13 @@ title: Each blocks
6
6
7
7
``` svelte
8
8
<ul>
9
- {#each cats as cat}
10
- <li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
11
- {cat.name}
12
- </a></li>
13
- {/each}
9
+ +++{#each cats as cat}+++
10
+ <li>
11
+ <a href="https://www.youtube.com/watch?v={cat.id}">
12
+ {cat.name}
13
+ </a>
14
+ </li>
15
+ +++{/each}+++
14
16
</ul>
15
17
```
16
18
@@ -19,10 +21,12 @@ title: Each blocks
19
21
第2引数として現在の * index* をこのように取得することができます。
20
22
21
23
``` svelte
22
- {#each cats as cat, i}
23
- <li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
24
- {i + 1}: {cat.name}
25
- </a></li>
24
+ {#each cats as cat+++, i}+++
25
+ <li>
26
+ <a href="https://www.youtube.com/watch?v={cat.id}">
27
+ +++{i + 1}:+++ {cat.name}
28
+ </a>
29
+ </li>
26
30
{/each}
27
31
```
28
32
Original file line number Diff line number Diff line change 19
19
20
20
<ul >
21
21
<!-- open each block -->
22
- <li >
23
- <a
24
- target =" _blank"
25
- href ="https://www.youtube.com/watch?v= {cat .id }"
26
- >
27
- {cat .name }
28
- </a >
29
- </li >
22
+ <li >
23
+ <a href ="https://www.youtube.com/watch?v= {cat .id }" >
24
+ {cat .name }
25
+ </a >
26
+ </li >
30
27
<!-- close each block -->
31
28
</ul >
Original file line number Diff line number Diff line change 20
20
<ul >
21
21
{#each cats as { id, name }, i }
22
22
<li >
23
- <a
24
- target =" _blank"
25
- href ="https://www.youtube.com/watch?v= {id }"
26
- >
23
+ <a href ="https://www.youtube.com/watch?v= {id }" >
27
24
{i + 1 }: {name }
28
25
</a >
29
26
</li >
Original file line number Diff line number Diff line change @@ -4,14 +4,14 @@ title: Keyed each blocks
4
4
5
5
デフォルトでは、` each ` ブロックの値を変更すると、ブロックの * 末尾* にアイテムを追加・削除し、変更された値を更新します。これはあなたが望むものではないかもしれません。
6
6
7
- 説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の ` <Thing> ` コンポーネントは削除されず、* 末尾の* DOM ノードが削除されます。そして残りの DOM ノードの ` name ` の値は更新されますが、絵文字は更新されません。
7
+ 説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の ` <Thing> ` コンポーネントは削除されず、* 末尾の* DOM ノードが削除されます。そして残りの DOM ノードの ` name ` の値は更新されますが、絵文字は更新されません。この絵文字は、 ` Thing.svelte ` が初期化されるときに固定されているのです。
8
8
9
9
代わりに、先頭の ` <Thing> ` コンポーネントとその DOM ノードだけを削除して、残りには影響を与えないようにしたいと思います。
10
10
11
11
そのためには、` each ` ブロックに一意な識別子 (または"key") を指定します。
12
12
13
13
``` svelte
14
- {#each things as thing (thing.id)}
14
+ {#each things as thing (+++ thing.id+++ )}
15
15
<Thing name={thing.name}/>
16
16
{/each}
17
17
```
Original file line number Diff line number Diff line change 1
1
<script >
2
- import { onDestroy } from ' svelte' ;
3
-
4
2
const emojis = {
5
3
apple: ' 🍎' ,
6
4
banana: ' 🍌' ,
12
10
// the name is updated whenever the prop value changes...
13
11
export let name;
14
12
15
- // ...but the "emoji" variable is fixed upon initialisation of the component
13
+ // ...but the "emoji" variable is fixed upon initialisation
14
+ // of the component because it uses `const` instead of `$:`
16
15
const emoji = emojis[name];
17
-
18
- // observe in the console which entry is removed
19
- onDestroy (() => {
20
- console .log (' thing destroyed: ' + name);
21
- });
22
16
</script >
23
17
24
18
<p >
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
<script >
2
2
async function getRandomNumber () {
3
- const res = await fetch (
4
- ` https://svelte.dev/tutorial/random-number`
5
- );
6
- const text = await res .text ();
3
+ // Fetch a random number between 0 and 100
4
+ // (with a delay, so that we can see it)
5
+ const res = await fetch (' /random-number' );
7
6
8
7
if (res .ok ) {
9
- return text;
8
+ return await res . text () ;
10
9
} else {
11
- throw new Error (text);
10
+ // Sometimes the API will fail!
11
+ throw new Error (' Request failed' );
12
12
}
13
13
}
14
14
19
19
}
20
20
</script >
21
21
22
- <button on:click ={handleClick }> generate random number </button >
22
+ <button on:click ={handleClick }>
23
+ generate random number
24
+ </button >
23
25
24
26
<!-- replace this element -->
25
27
<p >{promise }</p >
Original file line number Diff line number Diff line change
1
+ export async function GET ( req ) {
2
+ const query = req . url . searchParams ;
3
+ let min = query . get ( 'min' ) || '0' ;
4
+ let max = query . get ( 'max' ) || '100' ;
5
+ min = + min ;
6
+ max = + max ;
7
+
8
+ // simulate a long delay
9
+ await new Promise ( ( res ) => setTimeout ( res , 1000 ) ) ;
10
+
11
+ // fail sometimes
12
+ if ( Math . random ( ) < 0.333 ) {
13
+ return new Response ( `Failed to generate random number. Please try again` , {
14
+ status : 400 ,
15
+ headers : { 'Access-Control-Allow-Origin' : '*' }
16
+ } ) ;
17
+ }
18
+
19
+ const num = min + Math . round ( Math . random ( ) * ( max - min ) ) ;
20
+ return new Response ( String ( num ) , {
21
+ headers : { 'Access-Control-Allow-Origin' : '*' }
22
+ } ) ;
23
+ }
Original file line number Diff line number Diff line change 1
1
<script >
2
2
async function getRandomNumber () {
3
- const res = await fetch (
4
- ` https://svelte.dev/tutorial/random-number`
5
- );
6
- const text = await res .text ();
3
+ // Fetch a random number between 0 and 100
4
+ // (with a delay, so that we can see it)
5
+ const res = await fetch (' /random-number' );
7
6
8
7
if (res .ok ) {
9
- return text;
8
+ return await res . text () ;
10
9
} else {
11
- throw new Error (text);
10
+ // Sometimes the API will fail!
11
+ throw new Error (' Request failed' );
12
12
}
13
13
}
14
14
19
19
}
20
20
</script >
21
21
22
- <button on:click ={handleClick }> generate random number </button >
22
+ <button on:click ={handleClick }>
23
+ generate random number
24
+ </button >
23
25
24
26
{#await promise }
25
27
<p >...waiting</p >
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ title: DOM events
5
5
今までざっと見てきたように、` on: ` ディレクティブを使用して要素の任意のイベントをリスニングできます。
6
6
7
7
``` svelte
8
- <div on:mousemove={handleMousemove}>
8
+ <div +++ on:mousemove={handleMousemove}+++ >
9
9
The mouse position is {m.x} x {m.y}
10
10
</div>
11
11
```
You can’t perform that action at this time.
0 commit comments