- Học Vue
-
Hệ sinh thái
Hỗ trợ
Công cụ
Thư viện chính thức
Tin tức
Tài nguyên
- Đội ngũ
- Hỗ trợ Vue
- Ngôn ngữ
Hướng dẫn
Khái niệm cơ bản
- Cài đặt
- Giới thiệu
- Đối tượng Vue
- Cú pháp template
- Computed property và watcher
- Binding cho class và style
- Render theo điều kiện
- Render danh sách
- Xử lí sự kiện
- Ràng buộc form input
- Cơ bản về component
Components In-Depth
- Đăng kí Component
- Props
- Custom Events
- Slots
- Dynamic & Async Components
- Handling Edge Cases
Hiệu ứng chuyển động
- Transition cho enter/leave & danh sách
- Transition cho trạng thái
Tái sử dụng & kết hợp
- Mixin
- Directive tùy biến
- Các hàm render & JSX
- Plugin
- Filter
Công cụ
- Triển khai cho môi trường production
- Single File Components
- Unit test
- TypeScript Support
Mở rộng quy mô ứng dụng
- Routing
- Quản lí trạng thái
- Render ở phía server
Bên trong Vue
- Reactivity in Depth
Thông tin thêm
- Comparison with Other Frameworks
- Join the Vue.js Community!
- Đội ngũ
Slots
This page assumes you’ve already read the Components Basics. Read that first if you are new to components.
Slot Content
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.
This allows you to compose components like this:
<navigation-link url="/profile"> |
Then in the template for <navigation-link>
, you might have:
<a |
When the component renders, the <slot>
element will be replaced by “Your Profile”. Slots can contain any template code, including HTML:
<navigation-link url="/profile"> |
Or even other components:
<navigation-link url="/profile"> |
If <navigation-link>
did not contain a <slot>
element, any content passed to it would simply be discarded.
Named Slots
There are times when it’s useful to have multiple slots. For example, in a hypothetical base-layout
component with the following template:
<div class="container"> |
For these cases, the <slot>
element has a special attribute, name
, which can be used to define additional slots:
<div class="container"> |
To provide content to named slots, we can use the slot
attribute on a <template>
element in the parent:
<base-layout> |
Or, the slot
attribute can also be used directly on a normal element:
<base-layout> |
There can still be one unnamed slot, which is the default slot that serves as a catch-all outlet for any unmatched content. In both examples above, the rendered HTML would be:
<div class="container"> |
Default Slot Content
There are cases when it’s useful to provide a slot with default content. For example, a <submit-button>
component might want the content of the button to be “Submit” by default, but also allow users to override with “Save”, “Upload”, or anything else.
To achieve this, specify the default content in between the <slot>
tags.
<button type="submit"> |
If the slot is provided content by the parent, it will replace the default content.
Compilation Scope
When you want to use data inside a slot, such as in:
<navigation-link url="/profile"> |
That slot has access to the same instance properties (i.e. the same “scope”) as the rest of the template. The slot does not have access to <navigation-link>
‘s scope. For example, trying to access url
would not work. As a rule, remember that:
Everything in the parent template is compiled in parent scope; everything in the child template is compiled in the child scope.
Scoped Slots
New in 2.1.0+
Sometimes you’ll want to provide a component with a reusable slot that can access data from the child component. For example, a simple <todo-list>
component may contain the following in its template:
<ul> |
But in some parts of our app, we want the individual todo items to render something different than just the todo.text
. This is where scoped slots come in.
To make the feature possible, all we have to do is wrap the todo item content in a <slot>
element, then pass the slot any data relevant to its context: in this case, the todo
object:
<ul> |
Now when we use the <todo-list>
component, we can optionally define an alternative <template>
for todo items, but with access to data from the child via the slot-scope
attribute:
<todo-list v-bind:todos="todos"> |
In 2.5.0+,
slot-scope
is no longer limited to the<template>
element, but can instead be used on any element or component in the slot.
Destructuring slot-scope
The value of slot-scope
can actually accept any valid JavaScript expression that can appear in the argument position of a function definition. This means in supported environments (single-file components or modern browsers) you can also use ES2015 destructuring in the expression, like so:
<todo-list v-bind:todos="todos"> |
This is a great way to make scoped slots a little cleaner.