Return to site

Vue Js Slots Tutorial

broken image


Scoped slots are a useful feature of Vue.js that can make components more versatile and reusable. The only problem is they're difficult to understand! Trying to get your head around the interweaving of parent and child scopes is like solving a tough math equation.

A good approach when you can't understand something easily is to try put it to use in solving a problem. In this article, I'll demonstrate how I used scoped slots to build a reusable list component.

Note: You can see the finished product in this Codepen.

The basic component

Vue.js is a rapidly growing front-end framework for JavaScript, inspired by Angular.js, Reactive.js, and Rivets.js that offers simplistic user-interface design, manipulation, and deep reactivity. It is described as a MVVM patterned framework, Model-View View-Model, which is based on the concept of two-way binding data to components and views.

The component we're going to build is called my-list and it displays lists of things. The special feature is that you can customize how the list items are rendered in every usage of the component.

Again, this series is not a Vue.js full tutorial, so I'll let you refer to the excellent Vue documentation if you need a refresh on Vue events and (named and scoped) slots, but, really, as you. This is the second part in a five-part series about the JavaScript framework, Vue.js. In this part, we'll go over Components, Props, and Slots. This is not intended to be a complete guide, but rather an overview of the basics to get you up and running so you can get to know Vue.js and understand what the framework has to offer. The js directory – the transpiled code which is generated from our files in the src directory. The css directory – the compiled style from the SCSS files. The src directory and Vue template files. Inside this directory, there are two files: main.js and App.vue. The main.js file is the entry point for the application. It attaches the Vue app. Vue Slots Tutorial Vue has support for slots built in and it is based on the shadow dom. It is a specification that is still in development but worth taking a look at to learn more about how they work, should this development approach become more common.

Let's tackle the simplest use case first, and get my-list to render just one list of things: an array of geometric shape names and the number of sides they have.

app.js

index.html

Slots

With a bit of CSS added, that will look the following:

Generalizing my-list

Now we want to make my-list versatile enough to render any kind of list. The second test case will be a list of colors, including a small swatch to show what the color looks like.

To do this, we'll have to abstract any data specific to the shapes list. Since the items in our lists may be structured differently, we'll give my-list a slot so the parent can define how any particular list will display.

app.js

index.html

Slot antenna for hf. The basic design uses a rectangular U-slot microstrip patch antenna on suitable microwave substrates. The new antenna is a probe-fed rectangular microstrip patch antenna on a permittivity substrate.

Tutorials

Let's now create two instances of the my-list component in the root instance to display our two test case lists:

app.js

That will look like this:

Superficial components

What we've just created works fine, but is not great code. my-list is, by name, a component for displaying a list. But we've had to abstract all the logic for rendering the list into the parent. The component does little more than wrap the list with some presentational markup.

Given that there's still repeated code in both declarations of the component (i.e.

), it'd be great if we could delegate this to the component so it isn't so superficial.

Never miss a new post!

Get our latest post in your inbox every Tuesday by subscribing to the Vue.js Developers Newsletter .

This subscription also includes Vue.js Developers promotional emails. You can opt-out at any time. View our privacy policy .

This form is protected by reCAPTCHA. The Google privacy policy and terms of service apply.

Scoped slots

To allow us to do this, we'll use a scoped slot instead of a regular slot. Scoped slots allow you to pass a template to the slot instead of passing a rendered element. It's called a 'scoped' slot because although the template is rendered in the parent scope, it will have access to certain child data.

For example, a component child with a scoped slot might look like the following.

A parent that uses this component will declare a template element in the slot. This template element will have an attribute scope that names an alias object. Any props added to the slot (in the child's template) are available as properties of the alias object.

Renders as:

Using a scoped slot in my-list

Let's pass the list arrays to my-list as props. Then we can replace the slot with a scoped slot. That way my-list can be responsible for iterating the list items, but the parent can still define how each list item should display.

index.html

Now we get my-list to iterate the items. Inside the v-for loop, item is an alias to the current list item. Four queens poker room. We can create a slot and bind that list item to the slot using v-bind='item'.

app.js

index.html

Note: if you haven't seen v-bind used without an argument before, this will bind the properties of an entire object to the element. This is useful with scoped slots as often the objects you bind will have arbitrary properties which now don't need to be specified by name.

Now we'll return to our root instance and declare a template within the slot of my-list. Looking at the shapes list first, the template must include the scope property to which we assign an alias shape. This alias allows us to access the scoped props. Inside the template, we can use exactly the same markup we had before to display our shape list items.

Now here's the full template:

Conclusion

Vue Js Slots

Although this approach has just as much markup as before, it has delegated the common functionality to the component which makes for a more robust design.

Here's a Codepen of the complete code:

See the Pen Scoped Slots in Vue.js Components by Anthony (@anthonygore) on CodePen.

bySai gowtham

Casino niagara poker room phone number. In this tutorial, we will learn about how to use the slots in vue.js with the help of examples.

What are Slots?

Slots helps us to pass the data between opening and closing component tags.

In vue.js props are used to pass the data to its child components, but it is hard to pass when we have a complex code. In such cases slots can be used.

Vue Js Slots Tutorial

Let's create a new component called Post by adding the element.

Now, if we pass any content between the Post component opening and closing tags that are rendered in the place of element.

Output:

Named Slots

Sometimes, we need to pass the data to a specific places in such cases named slots can be used.

Vue

The named slots can be created by adding a name attribute to the element.

To pass the content to the named slots we need to use v-slot directive on template providing slot name as v-slot argument.

Fallback data

Vue Js Slots Tutorial Free

In some cases, we can use fallback data (aka default) when data is not passed to a slot.

For example:

In the above component, we have added a Submit text inside a slot element.

Now, If we use a my-button component without passing any data we can seethe fallback data Submit text is rendered inside the button.

Output of rendered html:

But, if we pass data to the my-button component fallback data is replaced.

Tutorial

With a bit of CSS added, that will look the following:

Generalizing my-list

Now we want to make my-list versatile enough to render any kind of list. The second test case will be a list of colors, including a small swatch to show what the color looks like.

To do this, we'll have to abstract any data specific to the shapes list. Since the items in our lists may be structured differently, we'll give my-list a slot so the parent can define how any particular list will display.

app.js

index.html

Slot antenna for hf. The basic design uses a rectangular U-slot microstrip patch antenna on suitable microwave substrates. The new antenna is a probe-fed rectangular microstrip patch antenna on a permittivity substrate.

Let's now create two instances of the my-list component in the root instance to display our two test case lists:

app.js

That will look like this:

Superficial components

What we've just created works fine, but is not great code. my-list is, by name, a component for displaying a list. But we've had to abstract all the logic for rendering the list into the parent. The component does little more than wrap the list with some presentational markup.

Given that there's still repeated code in both declarations of the component (i.e.

), it'd be great if we could delegate this to the component so it isn't so superficial.

Never miss a new post!

Get our latest post in your inbox every Tuesday by subscribing to the Vue.js Developers Newsletter .

This subscription also includes Vue.js Developers promotional emails. You can opt-out at any time. View our privacy policy .

This form is protected by reCAPTCHA. The Google privacy policy and terms of service apply.

Scoped slots

To allow us to do this, we'll use a scoped slot instead of a regular slot. Scoped slots allow you to pass a template to the slot instead of passing a rendered element. It's called a 'scoped' slot because although the template is rendered in the parent scope, it will have access to certain child data.

For example, a component child with a scoped slot might look like the following.

A parent that uses this component will declare a template element in the slot. This template element will have an attribute scope that names an alias object. Any props added to the slot (in the child's template) are available as properties of the alias object.

Renders as:

Using a scoped slot in my-list

Let's pass the list arrays to my-list as props. Then we can replace the slot with a scoped slot. That way my-list can be responsible for iterating the list items, but the parent can still define how each list item should display.

index.html

Now we get my-list to iterate the items. Inside the v-for loop, item is an alias to the current list item. Four queens poker room. We can create a slot and bind that list item to the slot using v-bind='item'.

app.js

index.html

Note: if you haven't seen v-bind used without an argument before, this will bind the properties of an entire object to the element. This is useful with scoped slots as often the objects you bind will have arbitrary properties which now don't need to be specified by name.

Now we'll return to our root instance and declare a template within the slot of my-list. Looking at the shapes list first, the template must include the scope property to which we assign an alias shape. This alias allows us to access the scoped props. Inside the template, we can use exactly the same markup we had before to display our shape list items.

Now here's the full template:

Conclusion

Vue Js Slots

Although this approach has just as much markup as before, it has delegated the common functionality to the component which makes for a more robust design.

Here's a Codepen of the complete code:

See the Pen Scoped Slots in Vue.js Components by Anthony (@anthonygore) on CodePen.

bySai gowtham

Casino niagara poker room phone number. In this tutorial, we will learn about how to use the slots in vue.js with the help of examples.

What are Slots?

Slots helps us to pass the data between opening and closing component tags.

In vue.js props are used to pass the data to its child components, but it is hard to pass when we have a complex code. In such cases slots can be used.

Let's create a new component called Post by adding the element.

Now, if we pass any content between the Post component opening and closing tags that are rendered in the place of element.

Output:

Named Slots

Sometimes, we need to pass the data to a specific places in such cases named slots can be used.

The named slots can be created by adding a name attribute to the element.

To pass the content to the named slots we need to use v-slot directive on template providing slot name as v-slot argument.

Fallback data

Vue Js Slots Tutorial Free

In some cases, we can use fallback data (aka default) when data is not passed to a slot.

For example:

In the above component, we have added a Submit text inside a slot element.

Now, If we use a my-button component without passing any data we can seethe fallback data Submit text is rendered inside the button.

Output of rendered html:

But, if we pass data to the my-button component fallback data is replaced.

Output of rendered html:





broken image