javascript / beginner
Snippet
Auto-subscription with $
Prefixing a store name with the $ symbol tells Svelte to automatically subscribe to changes and unsubscribe when the component is destroyed, preventing memory leaks.
snippet.js
1
2
3
4
5
<script>import { count } from './stores.js';</script><h1>The count is {$count}</h1>
svelte
Breakdown
1
import { count } from './stores.js';
Imports a store instance from an external JavaScript file.
2
{$count}
The $ prefix allows direct access to the store's value within the template and handles the subscription automatically.