Basic Svelte
Introduction
Bindings
Classes and styles
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
In Svelte, an application is composed from one or more components. A component is a reusable self-contained block of code that encapsulates HTML, CSS and JavaScript that belong together, written into a .svelte file. The App.svelte file, open in the code editor to the right, is a simple component.
Adding data
A component that just renders some static markup isn’t very interesting. Let’s add some data.
First, add a script tag to your component and declare a name variable:
App
<script>
let name = 'Svelte';
</script>
<h1>Hello world!</h1><script lang="ts">
let name = 'Svelte';
</script>
<h1>Hello world!</h1>Then, we can refer to name in the markup:
App
<h1>Hello {name}!</h1>Inside the curly braces, we can put any JavaScript we want. Try changing name to name.toUpperCase() for a shoutier greeting.
App
<h1>Hello {name.toUpperCase()}!</h1>previous next
1
2
<h1>Hello world!</h1>