svelte-runes

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Svelte Runes

Svelte Runes

Quick Start

快速开始

Which rune? Props:
$props()
| Bindable:
$bindable()
| Computed:
$derived()
| Side effect:
$effect()
| State:
$state()
Key rules: Runes are top-level only. $derived can be overridden (use
const
for read-only). Objects/arrays are deeply reactive by default; use
$state.raw
for large data replaced wholesale.
选择哪个Rune? Props:
$props()
| 可绑定:
$bindable()
| 计算属性:
$derived()
| 副作用:
$effect()
| 状态:
$state()
核心规则: Runes只能在顶层使用。$derived可以被重写 (使用
const
定义只读属性)。对象/数组默认是深度响应式的; 对于需要整体替换的大型数据,请使用
$state.raw

Example

示例

svelte
<script>
	let count = $state(0); // Mutable state
	const doubled = $derived(count * 2); // Computed (const = read-only)

	$effect(() => {
		console.log(`Count is ${count}`); // Side effect
	});
</script>

<button onclick={() => count++}>
	{count} (doubled: {doubled})
</button>
svelte
<script>
	let count = $state(0); // 可变状态
	const doubled = $derived(count * 2); // 计算属性(const = 只读)

	$effect(() => {
		console.log(`Count is ${count}`); // 副作用
	});
</script>

<button onclick={() => count++}>
	{count} (doubled: {doubled})
</button>

Reference Files

参考文件

  • reactivity-patterns.md - When to use each rune
  • component-api.md - $props, $bindable patterns
  • snippets-vs-slots.md - New snippet syntax
  • common-mistakes.md - Anti-patterns with fixes
For
@attach
and other template directives, see the svelte-template-directives skill.
  • reactivity-patterns.md - 何时 使用各个Rune
  • component-api.md - $props、$bindable 使用模式
  • snippets-vs-slots.md - 新的 代码片段语法
  • common-mistakes.md - 反模式及 修复方案
关于
@attach
和其他模板指令,请查看 svelte-template-directives技能文档。

Notes

注意事项

  • Use event properties like
    onclick
    , and
    {@render children()}
    in layouts
  • $derived
    can be reassigned (5.25+) - use
    const
    for read-only
  • Use
    createContext
    over
    setContext
    /
    getContext
    for type safety
  • Use
    $inspect.trace
    to debug reactivity issues
  • Prefer
    $derived.by
    for multi-line derivations
  • Avoid state updates inside
    $effect
    ; effects are an escape hatch
  • Effects do not run on the server; don't wrap effect bodies in
    if (browser)
  • Last verified: 2026-05-14
<!-- PROGRESSIVE DISCLOSURE GUIDELINES: - Keep this file ~50 lines total (max ~150 lines) - Use 1-2 code blocks only (recommend 1) - Keep description <200 chars for Level 1 efficiency - Move detailed docs to references/ for Level 3 loading - This is Level 2 - quick reference ONLY, not a manual LLM WORKFLOW (when editing this file): 1. Write/edit SKILL.md 2. Format (if formatter available) 3. Run: npx skills add . --list 4. If the skill is not discovered, check SKILL.md frontmatter formatting 5. Validate again to confirm -->
  • 在布局中使用
    onclick
    等事件属性,以及
    {@render children()}
    语法
  • $derived可被重新赋值(5.25+版本)- 使用
    const
    定义只读属性
  • 为保证类型安全,优先使用
    createContext
    而非
    setContext
    /
    getContext
  • 使用
    $inspect.trace
    调试响应式问题
  • 多行推导优先使用
    $derived.by
  • 避免在
    $effect
    内更新状态;副作用是一种兜底方案
  • 副作用不会在服务器端运行;无需用
    if (browser)
    包裹副作用代码
  • 最后验证时间: 2026-05-14
<!-- 渐进式披露指南: - 保持本文件总篇幅约50行(最多约150行) - 仅使用1-2个代码块(推荐1个) - Level 1效率要求:描述内容少于200字符 - 详细文档移至references/目录,用于Level 3加载 - 本文件为Level 2 - 仅作快速参考,而非完整手册 LLM工作流程(编辑本文件时): 1. 编写/编辑SKILL.md 2. 格式化(若有格式化工具) 3. 运行:npx skills add . --list 4. 若技能未被识别,检查SKILL.md前置元数据格式 5. 再次验证确认 -->