Skip to content

Commit ac8fcd5

Browse files
committed
document recursive component
1 parent 5edf60a commit ac8fcd5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

source/api/index.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,16 +828,27 @@ type: api
828828

829829
- **Details:**
830830

831-
When inspecting an extended Vue component in the console, the default constructor name is `VueComponent`, which isn't very informative. By passing in an optional `name` option to `Vue.extend()`, you will get a better inspection output so that you know which component you are looking at. The string will be camelized and used as the component's constructor name.
831+
Allow the component to recursively invoke itself in its template. Note that when a component is registered globally with `Vue.component()`, the global ID is automatically set as its name.
832+
833+
Another benefit of specifying a `name` option is console inspection. When inspecting an extended Vue component in the console, the default constructor name is `VueComponent`, which isn't very informative. By passing in an optional `name` option to `Vue.extend()`, you will get a better inspection output so that you know which component you are looking at. The string will be camelized and used as the component's constructor name.
832834

833835
- **Example:**
834836

835837
``` js
836838
var Ctor = Vue.extend({
837-
name: 'cool-stuff'
839+
name: 'stack-overflow',
840+
template:
841+
'<div>' +
842+
// recursively invoke self
843+
'<stack-overflow></stack-overflow>' +
844+
'</div>'
838845
})
846+
847+
// this will actually result in a max stack size exceeded
848+
// error, but let's assume it works...
839849
var vm = new Ctor()
840-
console.log(vm) // -> CoolStuff {$el: null, ...}
850+
851+
console.log(vm) // -> StackOverflow {$el: null, ...}
841852
```
842853

843854
## Instance Properties

source/guide/components.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,23 @@ export default {
802802
}
803803
```
804804

805+
### Recursive Component
806+
807+
Components can recursively invoke itself in its own template, however, it can only do so when it has the `name` option:
808+
809+
``` js
810+
var StackOverflow = Vue.extend({
811+
name: 'stack-overflow',
812+
template:
813+
'<div>' +
814+
// recursively invoke self
815+
'<stack-overflow></stack-overflow>' +
816+
'</div>'
817+
})
818+
```
819+
820+
A component like the above will result in a "max stack size exceeded" error, so make sure recursive invocation is conditional. When you register a component globally using `Vue.component()`, the global ID is automatically set as the component's `name` option.
821+
805822
### Fragment Instance
806823

807824
When you use the `template` option, the content of the template will replace the element the Vue instance is mounted on. It is therefore recommended to always include a single root-level element in templates.

0 commit comments

Comments
 (0)