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/v2/guide/components.md
+44-7
Original file line number
Diff line number
Diff line change
@@ -210,25 +210,64 @@ That's all you need to know about props for now, but once you've finished readin
210
210
When building out a `<blog-post>` component, your template will eventually contain more than just the title:
211
211
212
212
```html
213
-
<h3>{{ post.title }}</h3>
213
+
<h3>{{ title }}</h3>
214
214
```
215
215
216
216
At the very least, you'll want to include the post's content:
217
217
218
218
```html
219
-
<h3>{{ post.title }}</h3>
220
-
<divv-html="post.content"></div>
219
+
<h3>{{ title }}</h3>
220
+
<divv-html="content"></div>
221
221
```
222
222
223
223
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:
224
224
225
225
```html
226
226
<divclass="blog-post">
227
-
<h3>{{ post.title }}</h3>
228
-
<divv-html="post.content"></div>
227
+
<h3>{{ title }}</h3>
228
+
<divv-html="content"></div>
229
229
</div>
230
230
```
231
231
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
+
<pclass="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
+
232
271
## Sending Messages to Parents with Events
233
272
234
273
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', {
276
315
})
277
316
```
278
317
279
-
<pclass="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>
0 commit comments