在 [[Svelte]] 中通过 $derived
创建派生状态,和 [[$state(…)]] 不同,$derived
是只读的。
使用
在 count 更新后,doubled 会同步更新:
<script> let count = $state(0); let doubled = $derived(count * 2);</script>
<button onclick={() => count++}> {doubled}</button>
<p>{count} doubled is {doubled}</p>