• Latest
Step by Step Walkthrough of Your First Svelte Application

Step by Step Walkthrough of Your First Svelte Application

January 5, 2022
FCC filings reveal Apple’s ‘Network Adapter’ that runs iOS

FCC filings reveal Apple’s ‘Network Adapter’ that runs iOS

May 22, 2022
ETL, ELT, and Reverse ETL

ETL, ELT, and Reverse ETL

May 22, 2022
Poco Watch review – GSMArena.com news

Poco Watch review – GSMArena.com news

May 22, 2022
These Panasonic deals will save you up to $500 on cameras and lenses!

These Panasonic deals will save you up to $500 on cameras and lenses!

May 22, 2022
Weekly poll results: the Sony Xperia 1 IV and 10 IV are great, if you can afford them

Weekly poll results: the Sony Xperia 1 IV and 10 IV are great, if you can afford them

May 22, 2022
Applying Kappa Architecture to Make Data Available

Applying Kappa Architecture to Make Data Available

May 22, 2022
Nintendo Responds To Wii And DSi Shop Channel Outages

Nintendo Responds To Wii And DSi Shop Channel Outages

May 21, 2022
Poll: What’s The Best Warriors Game On Switch?

Poll: What’s The Best Warriors Game On Switch?

May 21, 2022
Question of the Week: Which woman photographer do you feel is/was most influential?

If you could spend one day with a famous photographer, who would it be?

May 21, 2022
Report: Apple tells suppliers it wants to expand manufacturing outside of China, India and Vietnam likely future production hubs

Report: Apple tells suppliers it wants to expand manufacturing outside of China, India and Vietnam likely future production hubs

May 21, 2022
The Centennial Case: A Shijima Story Review (Switch eShop)

The Centennial Case: A Shijima Story Review (Switch eShop)

May 21, 2022
Apple supplier BOE could lose millions of iPhone 14 OLED panel orders

Apple supplier BOE could lose millions of iPhone 14 OLED panel orders

May 21, 2022
Advertise with us
Sunday, May 22, 2022
Bookmarks
  • Login
  • Register
GetUpdated
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

Step by Step Walkthrough of Your First Svelte Application

January 5, 2022
in Software Development
Reading Time:3 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


In this post, we will do a step-by-step walkthrough of our first Svelte Application. In essence, we will be understanding the overall structure of a Svelte application.

If you wish to follow along, you can first set up a Svelte Rollup application so that you have a development project ready to work with.

1 – A Svelte Component

In Svelte, we can organize our application into components.

Let’s look at a simple component as below:

App.svelte<script>
    export let name;
</script>

<p>Welcome to the {name} Library</p>

As you can see, a component can have a script section and a normal HTML markup section. It can also have its own CSS style section but more on that later. Technically, all three sections are optional.

The script section contains the Javascript code that runs when we create an instance of this component. In the above snippet, we only have one statement within the script tags.

export let name;

The export keyword marks the variable as a prop of this component. In other words, it is accessible to the consumers of this component. Think of it as an interface to our component.

We can also specify an initial default value for a prop. The default value is used when the consumer does not specify the prop on the component. In other words, the value is undefined.

The markup section contains normal HTML code. See below:

<p>Welcome to the {name} Library</p>

However, notice the curly braces around the name. Basically, the curly braces specify that the value of the name will come from the prop name.

2 – The Main File

At this point, you might be wondering how our component App will be rendered. Also, how will it receive the prop values?

This will be taken care of in the main.js file. See below code:

main.jsimport App from './App.svelte';

const app = new App({
    target: document.body,
    props: {
        name: 'Fantasy'
    },
});

export default app;

As you can see, we first import the App component from the App.svelte file.

Next, we instantiate the component using the new keyword. While instantiating, we specify the target for the component. In this case, we want the App component to be rendered in the body of our HTML.

Also, we pass the props object. Notice that we use the same property name as the one inside the component. In the end, we export the App object.

3 – The Index HTML

There is another file we need to create in order to render our application. That file is the index.html. And we create the file within the public folder.

See below:

index.html<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My First Svelte App</title>
        <script defer src="https://dzone.com/articles/build/bundle.js"></script>
    </head>
    <body></body>
</html>

If you notice above, the body tags have no content. At the time of serving the application, the body section will be rendered with the App component as we specified in the main.js file.

The script tag points to the location of the bundle.js file. This is nothing but the compiled javascript file of our Svelte application. The browser cannot directly understand Svelte code. This is the reason we compile our Svelte code into browser-understandable javascript. You can read about it in the previous post.

Conclusion

With this, we have successfully done a step-by-step walkthrough of our first Svelte application. This post basically helps us attain a good understanding of the overall structure of a typical Svelte application.

In the next post, we will look at Svelte Reactive Variables.

You can find the code for this on Github.

In case of any queries or comments, please write in the comments section below.



Source link

ShareSendTweet
Previous Post

Random: Metroid Prime Hunters Composer Reflects On The Stressful Times At Nintendo

Next Post

‘Genshin Impact’ Version 2.4 Update Is Out Now With New Characters, Outfits, the Enkanomiya Map, and More – TouchArcade

Related Posts

ETL, ELT, and Reverse ETL

May 22, 2022
0
0
ETL, ELT, and Reverse ETL
Software Development

This is an article from DZone's 2022 Data Pipelines Trend Report.For more: Read the Report ETL (extract, transform, load) has...

Read more

Applying Kappa Architecture to Make Data Available

May 22, 2022
0
0
Applying Kappa Architecture to Make Data Available
Software Development

Introduction  Banks are accelerating their modernization effort to rapidly develop and deliver top-notch digital experiences for their customers. To achieve...

Read more
Next Post
‘Genshin Impact’ Version 2.4 Update Releases on January 5th with New Characters, Outfits, the Enkanomiya Map, and More – TouchArcade

‘Genshin Impact’ Version 2.4 Update Is Out Now With New Characters, Outfits, the Enkanomiya Map, and More – TouchArcade

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?