You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/index.md
+19-7Lines changed: 19 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -249,7 +249,8 @@ The component system is another important concept in Vue, because it's an abstra
249
249
In Vue, a component is essentially a Vue instance with pre-defined options. Registering a component in Vue is straightforward:
250
250
251
251
```js
252
-
Vue.component('todo', {
252
+
// Define a new component called todo-item
253
+
Vue.component('todo-item', {
253
254
template:'<li>This is a todo</li>'
254
255
})
255
256
```
@@ -258,14 +259,21 @@ Now you can compose it in another component's template:
258
259
259
260
```html
260
261
<ul>
261
-
<todov-for="todo in todos"></todo>
262
+
<!--
263
+
Create an instance of the todo-item component
264
+
for each todo in a todos array
265
+
-->
266
+
<todo-itemv-for="todo in todos"></todo-item>
262
267
</ul>
263
268
```
264
269
265
270
But this would render the same text for every todo, which is not super interesting. We should be able to pass data from the parent scope into child components. Let's modify the component definition to make it accept a [prop](/guide/components.html#Props):
266
271
267
272
```js
268
-
Vue.component('todo', {
273
+
Vue.component('todo-item', {
274
+
// The todo-item component now accepts a
275
+
// "prop", which is like a custom attribute.
276
+
// This prop is called todo.
269
277
props: ['todo'],
270
278
template:'<li>{{ todo.text }}</li>'
271
279
})
@@ -276,7 +284,11 @@ Now we can pass the todo into each repeated component using `v-bind`:
276
284
```html
277
285
<divid="app-7">
278
286
<ol>
279
-
<todov-for="todo in todos"v-bind:todo="todo"></todo>
287
+
<!--
288
+
Now we provide each todo-item with the todo object
289
+
it's representing, so that its content can be dynamic
290
+
-->
291
+
<todo-itemv-for="todo in todos"v-bind:todo="todo"></todo-item>
280
292
</ol>
281
293
</div>
282
294
```
@@ -291,11 +303,11 @@ var app7 = new Vue({
291
303
{% raw %}
292
304
<divid="app-7"class="demo">
293
305
<ol>
294
-
<todo v-for="todo in todos" v-bind:todo="todo"></todo>
306
+
<todo-item v-for="todo in todos" v-bind:todo="todo"></todo-item>
295
307
</ol>
296
308
</div>
297
309
<script>
298
-
Vue.component('todo', {
310
+
Vue.component('todo-item', {
299
311
props: ['todo'],
300
312
template:'<li>{{ todo.text }}</li>'
301
313
})
@@ -312,7 +324,7 @@ var app7 = new Vue({
312
324
</script>
313
325
{% endraw %}
314
326
315
-
This is just a contrived example, but we have managed to separate our app into two smaller units, and the child is reasonably well-decoupled from the parent via the props interface. We can now further improve our `<todo>` component with more complex template and logic without affecting the parent app.
327
+
This is just a contrived example, but we have managed to separate our app into two smaller units, and the child is reasonably well-decoupled from the parent via the props interface. We can now further improve our `<todo-item>` component with more complex template and logic without affecting the parent app.
316
328
317
329
In a large application, it is necessary to divide the whole app into components to make development manageable. We will talk a lot more about components [later in the guide](/guide/components.html), but here's an (imaginary) example of what an app's template might look like with components:
0 commit comments