01
はじめに
コンポーネント
Svelte のコンポーネントは、script・マークアップ・style を含む .svelte ファイルです。
svelte
<script>
let count = 0;
function increment() { count += 1; }
</script>
<button on:click={increment}>
Count: {count}
</button>リアクティビティ
$: ラベルでリアクティブな宣言ができます。
svelte
<script>
let count = 0;
$: doubled = count * 2;
</script>Props
export let で props を宣言します。
svelte
<script>
export let name = "World";
</script>
<h1>Hello {name}</h1>02
機能
Each
each ブロックでリストをレンダリングします。
svelte
<ul>
{#each items as item, i}
<li>{item.name}</li>
{/each}
</ul>If-Else
条件付きレンダリングです。
svelte
{#if condition}
<p>Yes</p>
{:else}
<p>No</p>
{/if}Stores
共有状態には Store を使います。$ プレフィックスで自動購読されます。
svelte
// store.js
import { writable } from "svelte/store";
export const count = writable(0);
// component
import { count } from "./store";
$count // access valueBindings
bind: で双方向バインディングができます。
svelte
<input bind:value={name}>
<input type="checkbox" bind:checked={active}>Was this helpful?