diff --git a/src/v2/guide/components.md b/src/v2/guide/components.md index ad5a4a9a0b..30968ba9eb 100644 --- a/src/v2/guide/components.md +++ b/src/v2/guide/components.md @@ -210,25 +210,64 @@ That's all you need to know about props for now, but once you've finished readin When building out a `` component, your template will eventually contain more than just the title: ```html -

{{ post.title }}

+

{{ title }}

``` At the very least, you'll want to include the post's content: ```html -

{{ post.title }}

-
+

{{ title }}

+
``` 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: ```html
-

{{ post.title }}

-
+

{{ title }}

+
``` +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: + +```html + +``` + +So this might be a good time to refactor the `` component to accept a single `post` prop instead: + +```html + +``` + +```js +Vue.component('blog-post', { + props: ['post'], + template: ` +
+

{{ post.title }}

+
+
+ ` +}) +``` + +

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.

+ +Now, whenever a new property is added to `post` objects, it will automatically be available inside ``. + ## Sending Messages to Parents with Events As we develop our `` 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', { }) ``` -

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.

- The problem is, this button doesn't do anything: ```html