<Info {...pkg}/>
{#if condition} {:else} {:else if condition} {/if}
{#each cats as { id, name }, index}
{#each [...iterable], index}
{#each cats as { id, name }, index}
{#each things as thing ([thing.id](http://thing.id/))}
{#await promise} {:then variable} {:catch error} {/await}
{#await promise then value}{/await}
<htmlTag on:event={handleEvent}> </htmlTag>
<div on:mousemove="{e => m = { x: e.clientX, y: e.clientY }}">
preventDefault
— calls event.preventDefault()
before running the handler. Useful for client-side form handling, for example.stopPropagation
— calls event.stopPropagation()
, preventing the event reaching the next elementpassive
— improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so)nonpassive
— explicitly set passive: false
capture
— fires the handler during the capture phase instead of the bubbling phase (MDN docs)once
— remove the handler after the first time it runsself
— only trigger handler if event.target is the element itselftrusted
— only trigger handler if event.isTrusted
is true
. I.e. if the event is triggered by a user action.<InnerComponent on:message />
<htmlTag on:click></htmlTag>
<input bind:value={name}>
not only will changes to the value of name update the input value, but changes to the input value will update name.<textarea bind:value></textarea>
<Keypad bind:value={pin} on:submit={handleSubmit}/>
onMount
rather than at the top level of the , lifecycle functions don't run during SSR, which means we can avoid fetching data that should be loaded lazily once the component has been mounted in the DOM.import { onMount } from 'svelte';
let photos = [];
onMount(async() => {
const res = await fetch(`https://jsonplaceholder.typicode.com/photos?_limit=20`);
photos = await res.json();
});
onDestroy()
메모리 누수 방지beforeUpdate
function schedules work to happen immediately before the DOM is updated. afterUpdate
is its counterpart, used for running code once the DOM is in sync with your data.beforeUpdate
will first run before the component has mounted, so we need to check for the existence of div before reading its properties.tick
function is unlike other lifecycle functions in that you can call it any time, not just when the component first initialises. It returns a promise that resolves as soon as any pending state changes have been applied to the DOM (or immediately, if there are no pending state changes).$variable
writable
store, which means it has set
and update
methods in addition to subscribe
.readable(initialValue, function start(set) { return function stop() {});
derived(baseValue, derivedValue)
import { writable } from 'svelte/store';
function createCount() {
const { subscribe, set, update } = writable(0);
return {
subscribe,
increment: () => update(n => n + 1),
decrement: () => update(n => n - 1),
reset: () => set(0)
};
}
export const count = createCount();
$name += '!'
assignment is equivalent to name.set($name + '!')
const { these, are, stores } = getContext(...);
setContext(key, context)
, then any child component can retrieve the context with const context = getContext(key)
.setContext
and getContext
must be called during component initialisation. Calling it afterwards - for example inside onMount
- will throw an error.const key = {}; export { key };
<script context="module">
block. Code contained inside it will run once, when the module first evaluates, rather than when a component is instantiated. Place this at the top of<svelte:self {...props}>
allows a component to contain itself recursively.<svelte:component this={selected.component}/>
A component can change its category altogether with svelte:component. Instead of a sequence of if block<svelte:window>
<svelte:window bind:scrollY={y}/>
innerWidth
innerHeight
outerWidth
outerHeight
scrollX
scrollY
online
— an alias for window.navigator.onLine
All except scrollX
and scrollY
are readonly.
<svelte:body on:mouseenter={handleMouseenter} on:mouseleave={handleMouseleave} />
<svelte:head><link rel="stylesheet" href="tutorial/dark-theme.css"></svelte:head>
<svelte:options/>
element allows you to specify compiler options.immutable={true}
— you never use mutable data, so the compiler can do simple referential equality checks to determine if values have changed
immutable={false}
— the default. Svelte will be more conservative about whether or not mutable objects have changed
accessors={true}
— adds getters and setters for the component's props
accessors={false}
— the default
namespace="..."
— the namespace where this component will be used, most commonly "svg"
tag="..."
— the name to use when compiling this component as a custom element
Consult the API reference for more information on these options.
svelte:fragment
element allows you to place content in a named slot without wrapping it in a container DOM element. This keeps the flow layout of your document intact.{@debug variable}