slotslucky

Resumo:

This page assumes you've already read the Components Basics. Read that first if you are

new to components.

Slot Content and0️⃣ Outlet ​

We have learned that components can accept

props, which can be JavaScript values of any type. But how about0️⃣ template content? In

some cases, we may want to pass a template fragment to a child component, and let the

0️⃣ child component render the fragment within its own template.

For example, we may have a

component that supports usage like0️⃣ this:

template < FancyButton > Click

me!

The template of looks like

this:

template <0️⃣ button class = "fancy-btn" > < slot >

button >

The element is0️⃣ a slot outlet that indicates where the parent-provided

slot content should be rendered.

And the final rendered DOM:

html < button class0️⃣ =

"fancy-btn" >Click me!

With slots, the is responsible for

rendering the outer ` }

Slot content is not just limited to0️⃣ text. It can be any valid template

content. For example, we can pass in multiple elements, or even other

components:

template0️⃣ < FancyButton > < span style = "color:red" >Click me! <

AwesomeIcon name = "plus" />

By using slots, our is more

flexible and reusable. We can now use it in different places with different0️⃣ inner

content, but all with the same fancy styling.

Vue components' slot mechanism is

inspired by the native Web Component 0️⃣ element, but with additional capabilities

that we will see later.

Render Scope ​

Slot content has access to the data scope of0️⃣ the

parent component, because it is defined in the parent. For example:

template < span >{{

message }} <0️⃣ FancyButton >{{ message }}

Here both {{ message

}} interpolations will render the same content.

Slot content does not have0️⃣ access to

the child component's data. Expressions in Vue templates can only access the scope it

is defined in, consistent0️⃣ with JavaScript's lexical scoping. In other

words:

Expressions in the parent template only have access to the parent scope;

expressions in0️⃣ the child template only have access to the child scope.

Fallback Content

There are cases when it's useful to specify fallback0️⃣ (i.e. default) content for a

slot, to be rendered only when no content is provided. For example, in a

0️⃣ component:

template < button type = "submit" > < slot >

We might

want the text "Submit"0️⃣ to be rendered inside the