Picture

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

new to components.

Slot Content and💶 Outlet ​

We have learned that components can accept

props, which can be JavaScript values of any type. But how about💶 template content? In

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

💶 child component render the fragment within its own template.

For example, we may have a

component that supports usage like💶 this:

template < FancyButton > Click

me!

The template of looks like

this:

template <💶 button class = "fancy-btn" > < slot >

button >

The element is💶 a slot outlet that indicates where the parent-provided

slot content should be rendered.

And the final rendered DOM:

html < button class💶 =

"fancy-btn" >Click me!

With slots, the is responsible for

rendering the outer ` }

Slot content is not just limited to💶 text. It can be any valid template

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

components:

template💶 < 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 different💶 inner

content, but all with the same fancy styling.

Vue components' slot mechanism is

inspired by the native Web Component 💶 element, but with additional capabilities

that we will see later.

Render Scope ​

Slot content has access to the data scope of💶 the

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

template < span >{{

message }} <💶 FancyButton >{{ message }}

Here both {{ message

}} interpolations will render the same content.

Slot content does not have💶 access to

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

is defined in, consistent💶 with JavaScript's lexical scoping. In other

words:

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

expressions in💶 the child template only have access to the child scope.

Fallback Content

There are cases when it's useful to specify fallback💶 (i.e. default) content for a

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

💶 component:

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

We might

want the text "Submit"💶 to be rendered inside the