VueJs Slots
Remarks
Important! Slots after render don't guarantee order for positions for slots. Slot, which was the first, may have a different position after render.
Examples
Using Single Slots
Single slots are used when a child component only defines one slot within its template. The page component above uses a single slot to distribute content.
An example of the page component's template using a single slot is below:
<html>
<head>
<title>Page Title</title>
</head>
<body>
<slot>
This will only be displayed if there is no content
to be distributed.
</slot>
</body>
</html>
To illustrate how the slot works we can set up a page as follows.
<page>
<p>This content will be displayed within the page component</p>
</page>
The end result will be:
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>This content will be displayed within the page component</p>
</body>
</html>
If we didn't put anything between the page tags an instead had
<html>
<head>
<title>Page Title</title>
</head>
<body>
This will only be displayed if there is no content
to be distributed.
</body>
</html>
What are slots?
Slots offer a convenient way of distributing content from a parent component to a child component. This content can be anything from text, HTML or even other components.
It can be helpful sometimes to think of slots as a means of injecting content directly into a child component's template.
Slots are especially useful when the component composition underneath the parent component isn't always the same.
Take the following example where we have a page component. The content of the page could
change based on whether that page displays e.g. an article, blog post or form.
Article
<page>
<article></article>
<comments></comments>
</page>
Blog Post
<page>
<blog-post></blog-post>
<comments></comments>
</page>
Form
<page>
<form></form>
</page
Notice how the content of the page component can change. If we didn't use slots this would be more difficult as the inner part of the template would be fixed.
Remember: "Everything in the parent template is compiled in parent scope; everything in the child template is compiled in child scope."