Skip to content

Commit 1febcd9

Browse files
author
ntepluhina
committed
Intro page finished
1 parent d009c97 commit 1febcd9

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

docs/.vuepress/components/intro-7.vue

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<div id="app-7" class="demo">
3+
<ol>
4+
<todo-item v-for="item in groceryList" v-bind:todo="item" :key="item.id"></todo-item>
5+
</ol>
6+
</div>
7+
</template>
8+
9+
<script>
10+
import Vue from 'vue/dist/vue.js'
11+
export default {
12+
data() {
13+
return {
14+
groceryList: [
15+
{ id: 0, text: 'Vegetables' },
16+
{ id: 1, text: 'Cheese' },
17+
{ id: 2, text: 'Whatever else humans are supposed to eat' }
18+
]
19+
}
20+
},
21+
components: {
22+
'todo-item': Vue.component('todo-item', {
23+
props: ['todo'],
24+
template: '<li>{{ todo.text }}</li>'
25+
})
26+
}
27+
}
28+
</script>
6.06 KB
Loading

docs/guide/introduction.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,110 @@ var app6 = new Vue({
171171
}
172172
})
173173
```
174-
<intro-6/>
174+
<intro-6/>
175+
176+
## Composing with Components
177+
178+
The component system is another important concept in Vue, because it's an abstraction that allows us to build large-scale applications composed of small, self-contained, and often reusable components. If we think about it, almost any type of application interface can be abstracted into a tree of components:
179+
180+
![Component Tree](/images/components.png)
181+
182+
In Vue, a component is essentially a Vue instance with pre-defined options. Registering a component in Vue is straightforward:
183+
184+
``` js
185+
// Define a new component called todo-item
186+
Vue.component('todo-item', {
187+
template: '<li>This is a todo</li>'
188+
})
189+
190+
const app = new Vue(...)
191+
```
192+
193+
Now you can compose it in another component's template:
194+
195+
```html
196+
<ol>
197+
<!-- Create an instance of the todo-item component -->
198+
<todo-item></todo-item>
199+
</ol>
200+
```
201+
202+
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](TODO:components.html#Props):
203+
204+
``` js
205+
Vue.component('todo-item', {
206+
// The todo-item component now accepts a
207+
// "prop", which is like a custom attribute.
208+
// This prop is called todo.
209+
props: ['todo'],
210+
template: '<li>{{ todo.text }}</li>'
211+
})
212+
```
213+
214+
Now we can pass the todo into each repeated component using `v-bind`:
215+
216+
``` html
217+
<div id="app-7">
218+
<ol>
219+
<!--
220+
Now we provide each todo-item with the todo object
221+
it's representing, so that its content can be dynamic.
222+
We also need to provide each component with a "key",
223+
which will be explained later.
224+
-->
225+
<todo-item
226+
v-for="item in groceryList"
227+
v-bind:todo="item"
228+
v-bind:key="item.id"
229+
></todo-item>
230+
</ol>
231+
</div>
232+
```
233+
``` js
234+
Vue.component('todo-item', {
235+
props: ['todo'],
236+
template: '<li>{{ todo.text }}</li>'
237+
})
238+
239+
const app7 = new Vue({
240+
el: '#app-7',
241+
data() {
242+
return {
243+
groceryList: [
244+
{ id: 0, text: 'Vegetables' },
245+
{ id: 1, text: 'Cheese' },
246+
{ id: 2, text: 'Whatever else humans are supposed to eat' }
247+
]
248+
}
249+
}
250+
})
251+
```
252+
<intro-7/>
253+
254+
This is 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.
255+
256+
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](TODO:components.md), but here's an (imaginary) example of what an app's template might look like with components:
257+
258+
``` html
259+
<div id="app">
260+
<app-nav></app-nav>
261+
<app-view>
262+
<app-sidebar></app-sidebar>
263+
<app-content></app-content>
264+
</app-view>
265+
</div>
266+
```
267+
268+
### Relation to Custom Elements
269+
270+
You may have noticed that Vue components are very similar to **Custom Elements**, which are part of the [Web Components Spec](https://www.w3.org/wiki/WebComponents/). That's because Vue's component syntax is loosely modeled after the spec. For example, Vue components implement the [Slot API](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.md) and the `is` special attribute. However, there are a few key differences:
271+
272+
1. The Web Components Spec has been finalized, but is not natively implemented in every browser. Safari 10.1+, Chrome 54+ and Firefox 63+ natively support web components. In comparison, Vue components don't require any polyfills and work consistently in all supported browsers (IE9 and above). When needed, Vue components can also be wrapped inside a native custom element.
273+
274+
2. Vue components provide important features that are not available in plain custom elements, most notably cross-component data flow, custom event communication and build tool integrations.
275+
276+
Although Vue doesn't use custom elements internally, it has [great interoperability](https://custom-elements-everywhere.com/#vue) when it comes to consuming or distributing as custom elements. Vue CLI also supports building Vue components that register themselves as native custom elements.
277+
278+
## Ready for More?
279+
280+
We've briefly introduced the most basic features of Vue.js core - the rest of this guide will cover them and other advanced features with much finer details, so make sure to read through it all!

0 commit comments

Comments
 (0)