|
| 1 | +--- |
| 2 | +title: 插槽 |
| 3 | +type: guide |
| 4 | +order: 104 |
| 5 | +--- |
| 6 | + |
| 7 | +> 该页面假设你已经阅读过了[组件基础](components.html)。如果你还对组件不太了解,推荐你先阅读它。 |
| 8 | +
|
| 9 | +## 插槽内容 |
| 10 | + |
| 11 | +Vue 实现了一套内容分发的 API,这套 API 基于当前的 [Web Components 规范草案](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.md),将 `<slot>` 元素作为承载分发内容的出口。 |
| 12 | + |
| 13 | +它允许你像这样合成组件: |
| 14 | + |
| 15 | +``` html |
| 16 | +<navigation-link url="/profile"> |
| 17 | + Your Profile |
| 18 | +</navigation-link> |
| 19 | +``` |
| 20 | + |
| 21 | +然后你在 `<navigation-link>` 的模板中可能会写为: |
| 22 | + |
| 23 | +``` html |
| 24 | +<a |
| 25 | + v-bind:href="url" |
| 26 | + class="nav-link" |
| 27 | +> |
| 28 | + <slot></slot> |
| 29 | +</a> |
| 30 | +``` |
| 31 | + |
| 32 | +当组件渲染的时候,这个 `<slot>` 元素将会被替换为“Your Profile”。插槽内可以包含任何模板代码,包括 HTML: |
| 33 | + |
| 34 | +``` html |
| 35 | +<navigation-link url="/profile"> |
| 36 | + <!-- 添加一个 Font Awesome 图标 --> |
| 37 | + <span class="fa fa-user"></span> |
| 38 | + Your Profile |
| 39 | +</navigation-link> |
| 40 | +``` |
| 41 | + |
| 42 | +甚至其它的组件: |
| 43 | + |
| 44 | +``` html |
| 45 | +<navigation-link url="/profile"> |
| 46 | + <!-- 添加一个图标的组件 --> |
| 47 | + <font-awesome-icon name="user"></font-awesome-icon> |
| 48 | + Your Profile |
| 49 | +</navigation-link> |
| 50 | +``` |
| 51 | + |
| 52 | +如果 `<navigation-link>` **没有**包含一个 `<slot>` 元素,则任何传入它的内容都会被抛弃。 |
| 53 | + |
| 54 | +## 具名插槽 |
| 55 | + |
| 56 | +有些时候我们需要多个插槽。例如,一个假设的 `<base-layout>` 组件多模板如下: |
| 57 | + |
| 58 | +``` html |
| 59 | +<div class="container"> |
| 60 | + <header> |
| 61 | + <!-- 我们希望把页头放这里 --> |
| 62 | + </header> |
| 63 | + <main> |
| 64 | + <!-- 我们希望把主要内容放这里 --> |
| 65 | + </main> |
| 66 | + <footer> |
| 67 | + <!-- 我们希望把页脚放这里 --> |
| 68 | + </footer> |
| 69 | +</div> |
| 70 | +``` |
| 71 | + |
| 72 | +对于这样的情况,`<slot>` 元素有一个特殊的特性:`name`。这个特性可以用来定义额外的插槽: |
| 73 | + |
| 74 | +``` html |
| 75 | +<div class="container"> |
| 76 | + <header> |
| 77 | + <slot name="header"></slot> |
| 78 | + </header> |
| 79 | + <main> |
| 80 | + <slot></slot> |
| 81 | + </main> |
| 82 | + <footer> |
| 83 | + <slot name="footer"></slot> |
| 84 | + </footer> |
| 85 | +</div> |
| 86 | +``` |
| 87 | + |
| 88 | +在向具名插槽提供内容的时候,我们可以在一个父组件的 `<template>` 元素上使用 `slot` 特性: |
| 89 | + |
| 90 | +```html |
| 91 | +<base-layout> |
| 92 | + <template slot="header"> |
| 93 | + <h1>Here might be a page title</h1> |
| 94 | + </template> |
| 95 | + |
| 96 | + <p>A paragraph for the main content.</p> |
| 97 | + <p>And another one.</p> |
| 98 | + |
| 99 | + <template slot="footer"> |
| 100 | + <p>Here's some contact info</p> |
| 101 | + </template> |
| 102 | +</base-layout> |
| 103 | +``` |
| 104 | + |
| 105 | +另一种 `slot` 特性的用法是直接用在一个普通的元素上: |
| 106 | + |
| 107 | +``` html |
| 108 | +<base-layout> |
| 109 | + <h1 slot="header">Here might be a page title</h1> |
| 110 | + |
| 111 | + <p>A paragraph for the main content.</p> |
| 112 | + <p>And another one.</p> |
| 113 | + |
| 114 | + <p slot="footer">Here's some contact info</p> |
| 115 | +</base-layout> |
| 116 | +``` |
| 117 | + |
| 118 | +我们还是可以保留一个未命名插槽,这个插槽是**默认插槽**,也就是说它会作为所有未匹配到插槽的内容的统一出口。上述两个示例渲染出来的 HTML 都将会是: |
| 119 | + |
| 120 | +``` html |
| 121 | +<div class="container"> |
| 122 | + <header> |
| 123 | + <h1>Here might be a page title</h1> |
| 124 | + </header> |
| 125 | + <main> |
| 126 | + <p>A paragraph for the main content.</p> |
| 127 | + <p>And another one.</p> |
| 128 | + </main> |
| 129 | + <footer> |
| 130 | + <p>Here's some contact info</p> |
| 131 | + </footer> |
| 132 | +</div> |
| 133 | +``` |
| 134 | + |
| 135 | +## 默认插槽的内容 |
| 136 | + |
| 137 | +有的时候为插槽提供默认的内容是很有用的。例如,一个 `<submit-button>` 组件可能希望这个按钮的默认内容是“Submit”,但是同时允许用户覆写为“Save”、“Upload”或别的内容。 |
| 138 | + |
| 139 | +你可以在 `<slot>` 标签内部指定默认的内容来做到这一点。 |
| 140 | + |
| 141 | +```html |
| 142 | +<button type="submit"> |
| 143 | + <slot>Submit</slot> |
| 144 | +</button> |
| 145 | +``` |
| 146 | + |
| 147 | +如果父组件为这个插槽提供了内容,则默认的内容会被替换掉。 |
| 148 | + |
| 149 | +## 编译作用域 |
| 150 | + |
| 151 | +当你想在插槽内使用数据时,例如: |
| 152 | + |
| 153 | +``` html |
| 154 | +<navigation-link url="/profile"> |
| 155 | + Logged in as {{ user.name }} |
| 156 | +</navigation-link> |
| 157 | +``` |
| 158 | + |
| 159 | +该插槽可以访问跟这个模板的其它地方相同的实例属性 (也就是说“作用域”是相同的)。但这个插槽**不能**访问 `<navigation-link>` 的作用域。例如尝试访问 `url` 是不会工作的。牢记一条准则: |
| 160 | + |
| 161 | +> 父组件模板的所有东西都会在父级作用域内编译;子组件模板的所有东西都会在子级作用域内编译。 |
| 162 | +
|
| 163 | +## 作用域插槽 |
| 164 | + |
| 165 | +> 2.1.0+ 新增 |
| 166 | +
|
| 167 | +有的时候你希望提供的组件带有一个可从子组件获取数据的可复用的插槽。例如一个简单的 `<todo-list>` 组件的模板可能包含了如下代码: |
| 168 | + |
| 169 | +```html |
| 170 | +<ul> |
| 171 | + <li |
| 172 | + v-for="todo in todos" |
| 173 | + v-bind:key="todo.id" |
| 174 | + > |
| 175 | + {{ todo.text }} |
| 176 | + </li> |
| 177 | +</ul> |
| 178 | +``` |
| 179 | + |
| 180 | +但是在我们应用的某些部分,我们希望每个独立的待办项渲染出和 `todo.text` 不太一样的东西。这也是作用域插槽的用武之地。 |
| 181 | + |
| 182 | +为了让这个特性成为可能,你需要做的全部事情就是将待办项内容包裹在一个 `<slot>` 元素上,然后将所有和其上下文相关的数据传递给这个插槽:在这个例子中,这个数据是 `todo` 对象: |
| 183 | + |
| 184 | +```html |
| 185 | +<ul> |
| 186 | + <li |
| 187 | + v-for="todo in todos" |
| 188 | + v-bind:key="todo.id" |
| 189 | + > |
| 190 | + <!-- 我们为每个 todo 准备了一个插槽,--> |
| 191 | + <!-- 将 `todo` 对象作为一个插槽的 prop 传入。--> |
| 192 | + <slot v-bind:todo="todo"> |
| 193 | + <!-- 回退的内容 --> |
| 194 | + {{ todo.text }} |
| 195 | + </slot> |
| 196 | + </li> |
| 197 | +</ul> |
| 198 | +``` |
| 199 | + |
| 200 | +现在当我们使用 `<todo-list>` 组件的时候,我们可以选择为待办项定义一个不一样的 `<template>` 作为替代方案,并且可以通过 `slot-scope` 特性从子组件获取数据: |
| 201 | + |
| 202 | +```html |
| 203 | +<todo-list v-bind:todos="todos"> |
| 204 | + <!-- 将 `slotProps` 定义为插槽作用域的名字 --> |
| 205 | + <template slot-scope="slotProps"> |
| 206 | + <!-- 为待办项自定义一个模板,--> |
| 207 | + <!-- 通过 `slotProps` 定制每个待办项。--> |
| 208 | + <span v-if="slotProps.todo.isComplete">✓</span> |
| 209 | + {{ slotProps.todo.text }} |
| 210 | + </template> |
| 211 | +</todo-list> |
| 212 | +``` |
| 213 | + |
| 214 | +> 在 2.5.0+,`slot-scope` 不再限制在 `<template>` 元素上使用,而可以用在插槽内的任何元素或组件上。 |
| 215 | +
|
| 216 | +### 解构 `slot-scope` |
| 217 | + |
| 218 | +如果一个 JavaScript 表达式在一个函数定义的参数位置有效,那么这个表达式实际上就可以被 `slot-scope` 接受。也就是说你可以在支持的环境下 ([单文件组件](single-file-components.html)或[现代浏览器](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#浏览器兼容)),在这些表达式中使用 [ES2015 解构语法](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#解构对象)。例如: |
| 219 | + |
| 220 | +```html |
| 221 | +<todo-list v-bind:todos="todos"> |
| 222 | + <template slot-scope="{ todo }"> |
| 223 | + <span v-if="todo.isComplete">✓</span> |
| 224 | + {{ todo.text }} |
| 225 | + </template> |
| 226 | +</todo-list> |
| 227 | +``` |
| 228 | + |
| 229 | +这会使作用域插槽变得更干净一些。 |
0 commit comments