Skip to content

Commit 418d7f3

Browse files
artiomgizachrisvfritz
authored andcommitted
Update components.md (#1632)
* Update components.md * Update components.md * improve flow of single root component section
1 parent e959d38 commit 418d7f3

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/v2/guide/components.md

+44-7
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,64 @@ That's all you need to know about props for now, but once you've finished readin
210210
When building out a `<blog-post>` component, your template will eventually contain more than just the title:
211211

212212
```html
213-
<h3>{{ post.title }}</h3>
213+
<h3>{{ title }}</h3>
214214
```
215215

216216
At the very least, you'll want to include the post's content:
217217

218218
```html
219-
<h3>{{ post.title }}</h3>
220-
<div v-html="post.content"></div>
219+
<h3>{{ title }}</h3>
220+
<div v-html="content"></div>
221221
```
222222

223223
If you try this in your template however, Vue will show an error, explaining that **every component must have a single root element**. You can fix this error by wrapping the template in a parent element, such as:
224224

225225
```html
226226
<div class="blog-post">
227-
<h3>{{ post.title }}</h3>
228-
<div v-html="post.content"></div>
227+
<h3>{{ title }}</h3>
228+
<div v-html="content"></div>
229229
</div>
230230
```
231231

232+
As our component grows, it's likely we'll not only need the title and content of a post, but also the published date, comments, and more. Defining a prop for each related piece of information could become very annoying:
233+
234+
```html
235+
<blog-post
236+
v-for="post in posts"
237+
v-bind:key="post.id"
238+
v-bind:title="post.title"
239+
v-bind:content="post.content"
240+
v-bind:publishedAt="post.publishedAt"
241+
v-bind:comments="post.comments"
242+
></blog-post>
243+
```
244+
245+
So this might be a good time to refactor the `<blog-post>` component to accept a single `post` prop instead:
246+
247+
```html
248+
<blog-post
249+
v-for="post in posts"
250+
v-bind:key="post.id"
251+
v-bind:post="post"
252+
></blog-post>
253+
```
254+
255+
```js
256+
Vue.component('blog-post', {
257+
props: ['post'],
258+
template: `
259+
<div class="blog-post">
260+
<h3>{{ post.title }}</h3>
261+
<div v-html="post.content"></div>
262+
</div>
263+
`
264+
})
265+
```
266+
267+
<p class="tip">The above example and some future ones use JavaScript's [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to make multi-line templates more readable. These are not supported by Internet Explorer (IE), so if you must support IE and are not transpiling (e.g. with Babel or TypeScript), use [newline escapes](https://css-tricks.com/snippets/javascript/multiline-string-variables-in-javascript/) instead.</p>
268+
269+
Now, whenever a new property is added to `post` objects, it will automatically be available inside `<blog-post>`.
270+
232271
## Sending Messages to Parents with Events
233272

234273
As we develop our `<blog-post>` component, some features may require communicating back up to the parent. For example, we may decide to include an accessibility feature to enlarge the text of blog posts, while leaving the rest of the page its default size:
@@ -276,8 +315,6 @@ Vue.component('blog-post', {
276315
})
277316
```
278317

279-
<p class="tip">The above example and some future ones use JavaScript's [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to make multi-line templates more readable. These are not supported by Internet Explorer (IE), so if you must support IE and are not transpiling (e.g. with Babel or TypeScript), use [newline escapes](https://css-tricks.com/snippets/javascript/multiline-string-variables-in-javascript/) instead.</p>
280-
281318
The problem is, this button doesn't do anything:
282319

283320
```html

0 commit comments

Comments
 (0)