Vuejs Slots Vs Props

  1. Vuejs Slots Vs Props Free
  2. Vuejs Slots Vs Props Play
  3. Vuejs Slots Vs Props For Real
  4. Vuejs Slots Vs Props Capcom

Note : Slots are supported only with vue-native-helper version 0.0.9 and above.

  • 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.
  • Slot props allow us to turn slots into reusable templates that can render different content based on input props. This is most useful when you are designing a reusable component that encapsulates data logic while allowing the consuming parent component to customize part of its layout.
  • A slot allows you to display an element in the child that is controlled by the parent. This helps make your components reusable. For example, let’s say you have a child component that displays data as a line chart, with the prop:chartData=“chartData”.

If you know about Vue slots, you might wonder if props and slots do the same thing. Well, the central idea of these tools or platforms is to encourage reusability and efficiency of resources. With that in mind, slots and props are similar.

Slot Content

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.

Props

In order to allow a parent component to pass elements into a child component, provide a <slot></slot> element inside the child component. Now, using the child component in the parent, any code in the parent’s <child> component will replace <slot/> in the child component.

This allows you to compose parent and child elements as shown :

If you run this, when the component renders, the <slot> element will be replaced by <text>Will render myself in the child component. You are welcome.</text>. Slots can contain any template code or even other components.

Vuejs Slots Vs Props Free

If child did not contain a <slot> element, any content passed to it would simply be discarded.

Named Slots

There are times when it’s convineint to have multiple slots.

Consider this parent element :

Specify the slot names in the attribute slot.

The <slot> element in the child component has a special attribute, name, which can be used to define named slots.

When the component renders, it will be in this order :

Notice that the order of the texts rendered corresponds to the slots arranged in the child component. The unnamed slot acts as the default slot.

Scoped Slots

Sometimes you’ll want to provide a component with a reusable slot that can access data from the child component. For example, when building a simple <todo-list> component, in some parts of the app, you may want the individual todo items to render something different than just what was defined in the <todo-list>. This is where scoped slots come in.

To understand this better let’s take a simple example, consider two components named parent and child, conveniently named to explain this better.

Both these components look like this:

A scope attribute can be used to receive the props for the slot from the child component. The data from the child element can be accessed as shown above with slotProps.texts.text.

Vuejs Slots Vs Props Play

The slot component is used in the child element and is bound through the v-bind to its data. The data that is bound here can be used in the parent component as shown.

Vuejs Slots Vs Props For Real

Slots

The two texts will be rendered in this manner :

Vuejs Slots Vs Props Capcom

Caught a mistake or want to contribute to the documentation?Edit this page on GitHub!