Skip to content

Commit 1aa0a44

Browse files
committed
add section about components and v-for
1 parent b793fc0 commit 1aa0a44

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/guide/components.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,26 @@ new Vue({
717717

718718
## Misc
719719

720+
### Components and v-for
721+
722+
You can directly use `v-for` on the custom component, like any normal element:
723+
724+
``` html
725+
<my-component v-for="item in items"></my-component>
726+
```
727+
728+
However, this won't pass any data to the component, because components have isolated scopes of their own. In order to pass the iterated data into the component, we should also use props:
729+
730+
``` html
731+
<my-component
732+
v-for="item in items"
733+
:item="item"
734+
:index="$index">
735+
</my-component>
736+
```
737+
738+
The reason for not automatically injecting `item` into the component is because that makes the component tightly coupled to how `v-for` works. Being explicit about where its data comes from makes the component reusable in other situations.
739+
720740
### Authoring Reusable Components
721741

722742
When authoring components, it is good to keep in mind whether you intend to reuse this component somewhere else later. It is OK for one-off components to have some tight coupling with each other, but reusable components should define a clean public interface.

0 commit comments

Comments
 (0)