You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compliler for creating reactive web apps & interfaces
Can be used for small parts of a site, or entirely (SPA)
Other framework differences
Svelte is a compiler not a framework
Svelte compiles your code for production at build time into a single, vanila, JavaScript bundle
No extra scripts or libraries are shipped to production
Often results in a faster running website
Basic Syntax
◼ User input & Data binding
<script>letname='Yoshi';letbeltColor='black';consthandleClick=()=>{beltColor='orange';};consthandleInput=(e)=>{beltColor=e.target.value;};</script>
// 밑의 두 코드는 같은 의미
<inputtype="text" on:input="{handleInput}" value="{beltColor}" /><inputtype="text" bind:value="{beltColor}" />
<script>letpeople=[{name: 'yoshi',beltColour: 'black',age: 25,id: 1},{name: 'mario',beltColour: 'orange',age: 45,id: 2},{name: 'luigi',beltColour: 'brown',age: 35,id: 3},];</script><main>
{#each people as person (person.id)}
<div><h4>{person.name}</h4><p>{person.age} years old, {person.beltColour} belt.</p></div>
{:else}
<p>There are no people to show...</p>
{/each}
</main>
◼ Inline event handlers
<script>letpeople=[{name: 'yoshi',beltColour: 'black',age: 25,id: 1},{name: 'mario',beltColour: 'orange',age: 45,id: 2},{name: 'luigi',beltColour: 'brown',age: 35,id: 3}];consthandleClick=(id)=>{// delete the person from peoplepeople=people.filter(person=>person.id!=id);}</script><main>
{#each people as person (person.id)}
<div><h4>{person.name}</h4><p>{person.age} years old, {person.beltColour} belt.</p><buttonon:click={() => handleClick(person.id)}>delete</button></div>
{:else}
<p>There are no people to show...</p>
{/each}
</main>
◼ Conditionals
<script>letnum=15;</script><main>
{#if num > 20}
<p>Greater than 20</p>
{:else if num > 5}
<p>Greater than 5</p>
{:else}
<p>Not Greater than 5</p>
{/if}
</main>
◼ CSS & Conditional styles
global.css에 작성한 스타일은 모든 component에 적용
component안에서 작성한 스타일은 해당 component에서만 적용
<script>letshowModal=true;letisPromo=true;</script>
{#if showModal}
<divclass="backdrop" class:promo="{isPromo}"><divclass="modal"><p>Sign up for offers!</p></div></div>
{/if}
◼ Props
// App.svelte
<Modalmessage="Hey, I am a prop value" isPromo="{true}" />
// Modal.svelte
<script>exportletmessage='default value';exportletisPromo=false;</script>
{#if showModal}
<divclass="backdrop" class:promo="{isPromo}"><divclass="modal"><p>{message}</p></div></div>
{/if}
◼ Event forwarding
component를 사용한 곳에서 이벤트를 달아주고 싶을 때 사용 (해당 component에서는 이벤트를 달아주기만 하면 됨)
// App.svelte
<Modalmessage="Hey, I am a prop value"
isPromo="{true}"
on:click="{toggleModal}"
/>
// Modal.svelte
<script>exportletmessage='default value';exportletisPromo=false;</script>
{#if showModal}
<divclass="backdrop" class:promo="{isPromo}" on:click><divclass="modal"><p>{message}</p></div></div>
{/if}
◼ Event modifiers
once: makes sure the event can only fire once (removes handler)
preventDefault: prevent the default action (run e.preventDefault())
self: only fires the event if the clicked element is the target