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}Store
store 用于共享状态;$ 前缀自动订阅。
svelte
// store.js
import { writable } from "svelte/store";
export const count = writable(0);
// component
import { count } from "./store";
$count // access value绑定
用 bind: 实现双向绑定。
svelte
<input bind:value={name}>
<input type="checkbox" bind:checked={active}>这篇内容对您有帮助吗?