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
66
77``` svelte
88<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}+++
1416</ul>
1517```
1618
@@ -19,10 +21,12 @@ title: Each blocks
1921第2引数として現在の * index* をこのように取得することができます。
2022
2123``` 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>
2630{/each}
2731```
2832
Original file line number Diff line number Diff line change 1919
2020<ul >
2121 <!-- 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 >
3027 <!-- close each block -->
3128</ul >
Original file line number Diff line number Diff line change 2020<ul >
2121 {#each cats as { id, name }, i }
2222 <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 }" >
2724 {i + 1 }: {name }
2825 </a >
2926 </li >
Original file line number Diff line number Diff line change @@ -4,14 +4,14 @@ title: Keyed each blocks
44
55デフォルトでは、` each ` ブロックの値を変更すると、ブロックの * 末尾* にアイテムを追加・削除し、変更された値を更新します。これはあなたが望むものではないかもしれません。
66
7- 説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の ` <Thing> ` コンポーネントは削除されず、* 末尾の* DOM ノードが削除されます。そして残りの DOM ノードの ` name ` の値は更新されますが、絵文字は更新されません。
7+ 説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の ` <Thing> ` コンポーネントは削除されず、* 末尾の* DOM ノードが削除されます。そして残りの DOM ノードの ` name ` の値は更新されますが、絵文字は更新されません。この絵文字は、 ` Thing.svelte ` が初期化されるときに固定されているのです。
88
99代わりに、先頭の ` <Thing> ` コンポーネントとその DOM ノードだけを削除して、残りには影響を与えないようにしたいと思います。
1010
1111そのためには、` each ` ブロックに一意な識別子 (または"key") を指定します。
1212
1313``` svelte
14- {#each things as thing (thing.id)}
14+ {#each things as thing (+++ thing.id+++ )}
1515 <Thing name={thing.name}/>
1616{/each}
1717```
Original file line number Diff line number Diff line change 11<script >
2- import { onDestroy } from ' svelte' ;
3-
42 const emojis = {
53 apple: ' 🍎' ,
64 banana: ' 🍌' ,
1210 // the name is updated whenever the prop value changes...
1311 export let name;
1412
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 `$:`
1615 const emoji = emojis[name];
17-
18- // observe in the console which entry is removed
19- onDestroy (() => {
20- console .log (' thing destroyed: ' + name);
21- });
2216 </script >
2317
2418<p >
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11<script >
22 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' );
76
87 if (res .ok ) {
9- return text;
8+ return await res . text () ;
109 } else {
11- throw new Error (text);
10+ // Sometimes the API will fail!
11+ throw new Error (' Request failed' );
1212 }
1313 }
1414
1919 }
2020 </script >
2121
22- <button on:click ={handleClick }> generate random number </button >
22+ <button on:click ={handleClick }>
23+ generate random number
24+ </button >
2325
2426<!-- replace this element -->
2527<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 11<script >
22 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' );
76
87 if (res .ok ) {
9- return text;
8+ return await res . text () ;
109 } else {
11- throw new Error (text);
10+ // Sometimes the API will fail!
11+ throw new Error (' Request failed' );
1212 }
1313 }
1414
1919 }
2020 </script >
2121
22- <button on:click ={handleClick }> generate random number </button >
22+ <button on:click ={handleClick }>
23+ generate random number
24+ </button >
2325
2426{#await promise }
2527 <p >...waiting</p >
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ title: DOM events
55今までざっと見てきたように、` on: ` ディレクティブを使用して要素の任意のイベントをリスニングできます。
66
77``` svelte
8- <div on:mousemove={handleMousemove}>
8+ <div +++ on:mousemove={handleMousemove}+++ >
99 The mouse position is {m.x} x {m.y}
1010</div>
1111```
You can’t perform that action at this time.
0 commit comments