diff --git a/src/v2/guide/components-slots.md b/src/v2/guide/components-slots.md new file mode 100644 index 000000000..0f35d5e9d --- /dev/null +++ b/src/v2/guide/components-slots.md @@ -0,0 +1,229 @@ +--- +title: 插槽 +type: guide +order: 104 +--- + +> 该页面假设你已经阅读过了[组件基础](components.html)。如果你还对组件不太了解,推荐你先阅读它。 + +## 插槽内容 + +Vue 实现了一套内容分发的 API,这套 API 基于当前的 [Web Components 规范草案](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.md),将 `` 元素作为承载分发内容的出口。 + +它允许你像这样合成组件: + +``` html + + Your Profile + +``` + +然后你在 `` 的模板中可能会写为: + +``` html + + + +``` + +当组件渲染的时候,这个 `` 元素将会被替换为“Your Profile”。插槽内可以包含任何模板代码,包括 HTML: + +``` html + + + + Your Profile + +``` + +甚至其它的组件: + +``` html + + + + Your Profile + +``` + +如果 `` **没有**包含一个 `` 元素,则任何传入它的内容都会被抛弃。 + +## 具名插槽 + +有些时候我们需要多个插槽。例如,一个假设的 `` 组件多模板如下: + +``` html +
+
+ +
+
+ +
+
+ +
+
+``` + +对于这样的情况,`` 元素有一个特殊的特性:`name`。这个特性可以用来定义额外的插槽: + +``` html +
+
+ +
+
+ +
+
+ +
+
+``` + +在向具名插槽提供内容的时候,我们可以在一个父组件的 `