<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
<style>
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
<p>This is a paragraph.</p>
<script>
import Nested from './Nested.svelte';
</script>
<Nested/>
https://github.com/sveltejs/integrations#bundler-plugins
<script>
let count = 0;
function handleClick() {count++;}
</script>
<button on:click={handleClick}>Clicked {count}</button>
let count = 0;
$: doubled = count * 2;
function addNumber() {
numbers.push(numbers.length + 1);
numbers = numbers; // assignment
}
<script>
export let answer;
</script>
<p>The answer is {answer}</p>
<script>
import Nested from './Nested.svelte';
</script>
<Nested answer={42}/>
<Info {...pkg}/>
{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{:else}
<button on:click={toggle}>
Log in
</button>
{/if}
{#each cats as cat}
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
{cat.name}
</a></li>
{/each}
{#each things as thing (thing.id)}
<Thing current={thing.color}/>
{/each}
{#await promise}
<p>...waiting</p>
{:then number}
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
<script>
let m = { x: 0, y: 0 };
function handleMousemove(event) {
m.x = event.clientX;
m.y = event.clientY;
}
</script>
<div on:mousemove={handleMousemove}>
The mouse position is {m.x} x {m.y}
</div>
<button on:click|once={handleClick}>
Click me
</button>
<script>
import {createEventDispatcher} from 'svelte';
const dispatch = createEventDispatcher();
function sayHello() {
dispatch('message', {
text: 'Hello...'
});
}
</script>
<button on:click={sayHello}>Hello</button>
<script>
import Inner from './Inner.svelte';
function handleMessage(event) {
alert(event.detail.text);
}
</script>
<Inner on:message={handleMessage}/>
<Inner on:message/> // we need to especially do this for HTML elements that need to send events outside
https://svelte.dev/tutorial/dom-event-forwarding
<input bind:value={name}>
<input type=number bind:value={a} min=0 max=10>