Skip to content

Commit f2daaff

Browse files
authored
Merge pull request volksbright#26 from vuejs/2.0
vm.$slots documentation improvement (vuejs#419)
2 parents 55f3db5 + e6e59c4 commit f2daaff

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/api/index.md

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -902,42 +902,47 @@ type: api
902902

903903
- **Details:**
904904

905-
A hash of VNode children of component that should resolve with slot. VNode children resolved with Single Slot are stored as the `default` key. VNode children resolve with Named Slot are stored as the key that specified with `slot` attribute. those VNode children are stored an Array.
905+
Used to access content [distributed by slots](/guide/components.html#Content-Distribution-with-Slots). Each [named slot](/guide/components.html#Named-Slots) has its own corresponding property (e.g. the contents of `slot="foo"` will be found at `vm.$slots.foo`). The `default` property contains any nodes not included in a named slot.
906+
907+
Accessing `vm.$slots` is most useful when writing a component with a [render function](/guide/render-function.html).
906908

907909
- **Example:**
908910

909911
```html
910-
<div id="slots-demo">
911-
...
912-
<my-component1>
913-
<p slot="slot1">named slot content1</p>
914-
<div>single slot content</div>
915-
<p slot="slot2">named slot content2</div></p>
916-
</my-component1>
917-
...
918-
</div>
912+
<blog-post>
913+
<h1 slot="header">
914+
About Me
915+
</h1>
916+
917+
<p>Here's some page content, which will be included in vm.$slots.default, because it's not inside a named slot.</p>
918+
919+
<p slot="footer">
920+
Copyright 2016 Evan You
921+
</p>
922+
923+
<p>If I have some content down here, it will also be included in vm.$slots.default.</p>.
924+
</blog-post>
919925
```
926+
920927
```js
921-
new Vue({
922-
...
923-
components: {
924-
'my-component1': {
925-
render: function (createElement) {
926-
console.log('single slot', this.$slots.default)
927-
console.log('named slot: slot1', this.$slots.slots1)
928-
console.log('named slot: slot2', this.$slots.slots1)
929-
return this.$slots.default
930-
}
931-
}
932-
},
933-
...
934-
}).$mount('#slots-demo')
928+
Vue.component('blog-post', {
929+
render: function (createElement) {
930+
var header = this.$slots.header
931+
var body = this.$slots.default
932+
var footer = this.$slots.footer
933+
return createElement('div', [
934+
createElement('header', header)
935+
createElement('main', body)
936+
createElement('footer', footer)
937+
])
938+
}
939+
})
935940
```
936941

937942
- **See also:**
938-
- [Render Functions](/guide/render-function.html)
943+
- [`<slot>` Component](#slot)
939944
- [Content Distribution with Slots](/guide/components.html#Content-Distribution-with-Slots)
940-
- [slot](#slot)
945+
- [Render Functions](/guide/render-function.html)
941946

942947
### vm.$refs
943948

0 commit comments

Comments
 (0)