By the end of this lesson, you will be able to:
:information_source: What is Svelte REPL? Svelte REPL is an online coding environment where you can test Svelte code instantly. Think of it as a playground where you can practice without installing anything on your computer!
You can start learning Svelte right away! Just visit Svelte REPL and begin coding.
:bulb: Tip Want to save your work? Log in with your GitHub account to save and share your projects. But you can practice without logging in too!
Need a GitHub account? Check out the Technical Skills Guidelines --- GitHub Account for help.
You can rename your project to anything you want:
Save your work by clicking the save button:
Find all your saved projects in your profile:
:memo: Note Sharing is easy! Once you save a project, you can share the URL with friends or teachers!
:bulb: Tip Best Practices for Svelte REPL:
- Use it for practice and learning - not big projects
- Perfect for testing small code snippets
- You can't delete saved projects (but you can clear the code and save it blank)
:information_source: What are Components? Components are reusable building blocks for your web app. Think of them like LEGO(R) blocks - you can combine them to build amazing things!
Svelte component files always end with .svelte
:
app.svelte
- Your main componentbutton.svelte
- A button componentheader.svelte
- A header componentComponents help you:
Examples of components:
:memo: Note Remember: The
app.svelte
file is special - it's your main component where all other components come together!
Every Svelte component can have three sections:
Here's a complete example:
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
<style>
h1 {
background-color: yellow;
}
</style>
Let's break it down:
<script>
tag - Where you write JavaScript code<h1>
tag - Your HTML content (you can add many HTML tags!)<style>
tag - Where you write CSS for stylingStyling in Svelte is super easy! You write CSS just like you're used to.
Let's start simple with just HTML:
<h1>Welcome!</h1>
<p>This is a paragraph.</p>
Now let's make it colorful:
<h1>Welcome!</h1>
<p>This is a paragraph.</p>
<style>
h1 {
background-color: yellow;
}
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
:bulb: Tip Component styles are special! The CSS you write in one component won't affect other components. Each component keeps its styles to itself!
Let's create a variable and use it:
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
:memo: Note Why use
let
instead ofvar
? We uselet
because it's safer! It helps prevent mistakes by keeping variables where they belong. Want to learn more? Check out this detailed explanation.
See those curly brackets {name}
? They're special!
{name}
with the value 'world'You can use variables in HTML attributes too:
<script>
let src = 'image.gif';
let name = 'example';
</script>
<img {src} alt={name}>
:bulb: Tip Shortcut! When the attribute name matches the variable name, you can write
{src}
instead ofsrc={src}
. It's the same thing!
Now for the fun part - combining components together!
:information_source: Key Concept:
app.svelte
is your main component. All other components get imported here to build your app!
To use a component, you need to import it first:
<script>
import ComponentName from 'componentPath';
</script>
First, create a simple button component:
Button.svelte
<button>Click Me</button>
Then import and use it in your app:
<script>
import Button from './Button.svelte';
</script>
<p>This is a paragraph.</p>
<Button />
:memo: Note Important! Component names must start with a capital letter when you import them!
Here's a helpful reference:
Component File | Import Syntax | HTML Tag |
---|---|---|
button.svelte |
import Button from './button.svelte'; |
<Button /> |
header.svelte |
import Header from './header.svelte'; |
<Header /> |
myForm.svelte |
import MyForm from './myForm.svelte'; |
<MyForm /> |
Let's review what you learned today:
Svelte REPL - Your online playground for coding
Components - Building blocks of your app
.svelte
extensionThree Parts of Components
<script>
- JavaScript logic<style>
- CSS stylingUsing Variables
{variableName}
in HTMLImporting Components
app.svelte
is your main componentReady to practice? Try these activities:
Create Your First Component
Build a Button Component
Button.svelte
fileapp.svelte
Experiment with Variables
:bulb: Tip Challenge yourself! Can you create a component that uses all three parts (JavaScript, HTML, and CSS) together?