Practice and reinforce the concepts from Lesson 2
In this hands-on activity, you will:
Total Time: 45-60 minutes
:bulb: Tip The Svelte REPL automatically saves your work. You can share your project by copying the URL from your browser!
Set up your file structure as shown in the image below:
Time: 10 minutes
Let's create your first Svelte component with styling!
app.svelte
file, add a <h1>
tag with the text "Welcome to Svelte!"<style>
tag<h1>
:
background-color: OrangeRed;
color: white;
padding: 16px 24px;
:bulb: Tip In Svelte, styles written in a component are automatically scoped to that component only. This means your h1 styles won't affect other components!
Expected Code:
<h1>Welcome to Svelte!</h1>
<style>
h1 \{
background-color: OrangeRed;
color: white;
padding: 16px 24px;
\}
</style>
Your Result Should Look Like This:
Time: 10 minutes
Now let's learn how to use JavaScript variables in Svelte components!
app.svelte
file, add a <script>
taglet src = '/tutorial/image.gif';
<h1>
tag (but above the <style>
tag), add an image:
<img>
tag with src=\{src\}
alt="dance"
attribute:bulb: Tip The curly braces
\{\}
tell Svelte to use the JavaScript variable value. You can also use the shorthand\{src\}
instead ofsrc=\{src\}
when the attribute name matches the variable name!
Expected Code:
<script>
let src = '/tutorial/image.gif';
</script>
<h1>Welcome to Svelte!</h1>
<img \{src\} alt="dance">
<style>
h1 \{
background-color: OrangeRed;
color: white;
padding: 16px 24px;
\}
</style>
Your Result Should Look Like This:
Time: 15 minutes
Let's practice component-based architecture by creating a separate component!
About.svelte
About.svelte
, add: <p>Let's dance!</p>
app.svelte
:
<script>
tag: import About from "./About.svelte";
<img>
tag, add: <About />
:bulb: Tip Component names must start with a capital letter in Svelte. This is how Svelte distinguishes between HTML elements and custom components!
Expected Code:
About.svelte:
<p>Let's dance!</p>
app.svelte:
<script>
import About from "./About.svelte";
let src = '/tutorial/image.gif';
</script>
<h1>Welcome to Svelte!</h1>
<img \{src\} alt="dance">
<About />
<style>
h1 \{
background-color: OrangeRed;
color: white;
padding: 16px 24px;
\}
</style>
Your Result Should Look Like This:
Time: 15 minutes
Let's create a more complex component with its own variables and styling!
Footer.svelte
Footer.svelte
:
<script>
tag with: let name = "Rick";
<footer>
tag containing: <p>Copyright © 2022 \{name\}</p>
<style>
tag with footer styling:
background-color: OrangeRed;
color: white;
padding: 8px 24px;
app.svelte
:
<Footer />
at the bottom:bulb: Tip Each Svelte component can have its own script, markup, and styles. The styles are automatically scoped, so your footer styles won't affect other components!
Expected Code:
Footer.svelte:
<script>
let name = "Rick";
</script>
<footer>
<p>Copyright © 2022 \{name\}</p>
</footer>
<style>
footer \{
background-color: OrangeRed;
color: white;
padding: 8px 24px;
\}
</style>
app.svelte (Final Version):
<script>
import About from "./About.svelte";
import Footer from "./Footer.svelte";
let src = '/tutorial/image.gif';
</script>
<h1>Welcome to Svelte!</h1>
<img \{src\} alt="dance">
<About />
<Footer />
<style>
h1 \{
background-color: OrangeRed;
color: white;
padding: 16px 24px;
\}
</style>
Your Final Result Should Look Like This:
Before submitting, ensure your project includes:
let
syntaxHaving issues? Check these common problems:
/tutorial/image.gif
(note the leading slash)\{variable\}
not double:warning: Warning Before submitting:
- Test all four activities in your Svelte REPL
- Make sure all components display correctly
- Copy your project URL from the browser address bar
When you have completed your "Svelte Components" project, submit it using the link below:
Code Review: YouTube Tutorial
:information_source: Info Need help? Remember to:
- Review the troubleshooting section above
- Check that your code matches the expected examples
- Watch the code review video for additional guidance