Vuejs Slots Vs Props

Posted on

If you have no idea how to proceed, the good news is that after reading this article, you will understand most of the things you should know about a handy Vue.js feature: slots. When I started with Vue.js, I was not using slots very much. But to be honest with you, now, it’s one of my favorite features. It makes things so much easier when.

Tutorial

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.

SlotsVuejs slots vs props free

Vuejs Slots Vs Props For Real

Vue.js has an amazing feature called Slots that’s modeled after the current Web Components spec draft. If a component is using slots then we can fill whatever content we want in those slots. This way the component can deal with all the logic and let the component’s user provide the view part of it. Vue.js has slots in order to make components have a redefinable structure, but by themselves they're pretty limited. Sometimes you need some data or state in order to define how a component should render. If you don't know slots, I suggest you learn them first on the Vue.js docs. Scoped slots is an advanced feature in Vue.js that allows you to. When crafting components with Vue.js you can go multiple ways. You can go with the props based approach or you can choose the slot based solution. If you’re using props, things can quickly get out. Note: Slots are supported only with vue-native-helper version 0.0.9 and above. Vue Native just like Vue implements a content distribution API that’s modeled after the current Web Components spec draft, using the slot element to serve as distribution outlets for content.

While basic component slots are all fine and dandy, sometimes you want the template inside the slot to be able to access data from the child component hosting the slot content. For example, if you’re trying to allow custom templates in a container while still retaining access to those containers’ data properties, you’ll want to use a scoped slot.

Introduction

Vuejs Slots Vs Props Play

Scoped slots are a new feature introduced in Vue 2.1.0. They allow you to pass properties from your child components into a scoped slot, and access them from the parent. Sort of like reverse property passing.

The first step to creating a scoped component slot is to pass properties into a default or named slot from your child component, like so:

Then, to use those properties inside a parent component’s slot content, create a template element tied to the slot. Add a scope attribute to the template element and set it to the name that you wish to access the scope properties from. This essentially creates a local variable for anything inside that template, allowing you to access it as if it was in the parent’s scope.

(For example, scope=“myScope”, properties passed into the <slot> will be accessible as {{myScope.myProperty}} while scope=“yourScope” will be accessed as {{yourScope.myProperty}}.)

Note: The template element does not get added to the DOM. It’s simply a wrapper.

Vuejs slots vs props playVuejs
ParentComponent.vue

If you’re using Vue 2.5 or above, use the slot-scope attribute instead of scope. Also you can skip the template elements.