svelte-runes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSvelte Runes
Svelte Runes
Quick Start
快速开始
Which rune? Props: | Bindable: |
Computed: | Side effect: | State:
$props()$bindable()$derived()$effect()$state()Key rules: Runes are top-level only. $derived can be overridden
(use for read-only). Objects/arrays are deeply reactive by
default; use for large data replaced wholesale.
const$state.raw选择哪个Rune? Props: | 可绑定: |
计算属性: | 副作用: | 状态:
$props()$bindable()$derived()$effect()$state()核心规则: Runes只能在顶层使用。$derived可以被重写
(使用定义只读属性)。对象/数组默认是深度响应式的;
对于需要整体替换的大型数据,请使用。
const$state.rawExample
示例
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
Forand other template directives, see the svelte-template-directives skill.@attach
- reactivity-patterns.md - 何时 使用各个Rune
- component-api.md - $props、$bindable 使用模式
- snippets-vs-slots.md - 新的 代码片段语法
- common-mistakes.md - 反模式及 修复方案
关于和其他模板指令,请查看 svelte-template-directives技能文档。@attach
Notes
注意事项
- Use event properties like , and
onclickin layouts{@render children()} - can be reassigned (5.25+) - use
$derivedfor read-onlyconst - Use over
createContext/setContextfor type safetygetContext - Use to debug reactivity issues
$inspect.trace - Prefer for multi-line derivations
$derived.by - Avoid state updates inside ; effects are an escape hatch
$effect - Effects do not run on the server; don't wrap effect bodies in
if (browser) - Last verified: 2026-05-14
- 在布局中使用等事件属性,以及
onclick语法{@render children()} - $derived可被重新赋值(5.25+版本)- 使用定义只读属性
const - 为保证类型安全,优先使用而非
createContext/setContextgetContext - 使用调试响应式问题
$inspect.trace - 多行推导优先使用
$derived.by - 避免在内更新状态;副作用是一种兜底方案
$effect - 副作用不会在服务器端运行;无需用包裹副作用代码
if (browser) - 最后验证时间: 2026-05-14