Intro to Svelte.js Components

Intro to Svelte.js Components

In this post we will give you information about Intro to Svelte.js Components. Hear we will give you detail about Intro to Svelte.js ComponentsAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to create components in svelte.

Components

Components are the reusable piece of UI where we can create once and reuse throught in our app.

In svelte we can create components by creating a file with .svelte extension each svelte component consists of html,css and javascript.

Let’s write our first svelte component.

MyfirstComponent.svelte
<script>    let name = "component"</script><style>   h1{       color:red;   }</style><h1>My first {name}</h1>

In the above code we have created our component called MyfirstComponent.svelte where it containshtml,css and javascript.

script: In the script tag, we need to write javascript related code.

style: In style tag, we need to write CSS code (the styles are scoped).

The { } brace is used to interpolate the javascript data into the markup.

Note: The data present inside the script can only be accessible inside the component.

Nested components

It is hard to build an entire app inside a single component so that we can import and use one component inside other components. Let’s see an example.

MysecondComponent.svelte
&lt;script&gt;    <span>import MyfirstComponent from './MyfirstComponent.svelte'&lt;/script&gt;&lt;style&gt;<span>&lt;/style&gt;&lt;div&gt;    &lt;h1&gt;My second component&lt;/h1&gt;    <span>&lt;MyfirstComponent/&gt;&lt;/div&gt;

In script we have imported the MyfirstComponent and added it to the markup just like html element.

Hope this code and post will helped you for implement Intro to Svelte.js Components. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US