Skip to content

Commit ae5912c

Browse files
skirtles-codefimion
authored andcommitted
Revise component-basics.md (#740)
* docs: revise component-basics.md * docs: reword a sentence to avoid repeating 'component instance'
1 parent 23d5b58 commit ae5912c

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/guide/component-basics.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ app.mount('#components-demo')
4040

4141
<common-codepen-snippet title="Component basics" slug="abORVEJ" tab="js,result" :preview="false" />
4242

43-
Since components are reusable instances, they accept the same options as a root instance, such as `data`, `computed`, `watch`, `methods`, and lifecycle hooks. The only exceptions are a few root-specific options like `el`.
43+
Since components are reusable instances, they accept the same options as a root instance, such as `data`, `computed`, `watch`, `methods`, and lifecycle hooks.
4444

4545
## Reusing Components
4646

@@ -66,7 +66,7 @@ It's common for an app to be organized into a tree of nested components:
6666

6767
For example, you might have components for a header, sidebar, and content area, each typically containing other components for navigation links, blog posts, etc.
6868

69-
To use these components in templates, they must be registered so that Vue knows about them. There are two types of component registration: **global** and **local**. So far, we've only registered components globally, using `component` method of created app:
69+
To use these components in templates, they must be registered so that Vue knows about them. There are two types of component registration: **global** and **local**. So far, we've only registered components globally, using the `component` method of our app:
7070

7171
```js
7272
const app = Vue.createApp({})
@@ -76,15 +76,15 @@ app.component('my-component-name', {
7676
})
7777
```
7878

79-
Globally registered components can be used in the template of `app` instance created afterwards - and even inside all subcomponents of that root instance's component tree.
79+
Globally registered components can be used in the template of any component within the app.
8080

8181
That's all you need to know about registration for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Component Registration](component-registration.md).
8282

8383
## Passing Data to Child Components with Props
8484

8585
Earlier, we mentioned creating a component for blog posts. The problem is, that component won't be useful unless you can pass data to it, such as the title and content of the specific post we want to display. That's where props come in.
8686

87-
Props are custom attributes you can register on a component. When a value is passed to a prop attribute, it becomes a property on that component instance. To pass a title to our blog post component, we can include it in the list of props this component accepts, using a `props` option:
87+
Props are custom attributes you can register on a component. To pass a title to our blog post component, we can include it in the list of props this component accepts, using the `props` option:
8888

8989
```js
9090
const app = Vue.createApp({})
@@ -97,7 +97,9 @@ app.component('blog-post', {
9797
app.mount('#blog-post-demo')
9898
```
9999

100-
A component can have as many props as you'd like and by default, any value can be passed to any prop. In the template above, you'll see that we can access this value on the component instance, just like with `data`.
100+
When a value is passed to a prop attribute, it becomes a property on that component instance. The value of that property is accessible within the template, just like any other component property.
101+
102+
A component can have as many props as you like and, by default, any value can be passed to any prop.
101103

102104
Once a prop is registered, you can pass data to it as a custom attribute, like this:
103105

@@ -175,7 +177,7 @@ Which can be used in the template to control the font size of all blog posts:
175177

176178
```html
177179
<div id="blog-posts-events-demo">
178-
<div v-bind:style="{ fontSize: postFontSize + 'em' }">
180+
<div :style="{ fontSize: postFontSize + 'em' }">
179181
<blog-post v-for="post in posts" :key="post.id" :title="post.title"></blog-post>
180182
</div>
181183
</div>
@@ -205,7 +207,7 @@ The problem is, this button doesn't do anything:
205207
</button>
206208
```
207209

208-
When we click on the button, we need to communicate to the parent that it should enlarge the text of all posts. Fortunately, component instances provide a custom events system to solve this problem. The parent can choose to listen to any event on the child component instance with `v-on` or `@`, just as we would with a native DOM event:
210+
When we click on the button, we need to communicate to the parent that it should enlarge the text of all posts. To solve this problem, component instances provide a custom events system. The parent can choose to listen to any event on the child component instance with `v-on` or `@`, just as we would with a native DOM event:
209211

210212
```html
211213
<blog-post ... @enlarge-text="postFontSize += 0.1"></blog-post>
@@ -219,11 +221,11 @@ Then the child component can emit an event on itself by calling the built-in [**
219221
</button>
220222
```
221223

222-
Thanks to the `@enlarge-text="postFontSize += 0.1"` listener, the parent will receive the event and update `postFontSize` value.
224+
Thanks to the `@enlarge-text="postFontSize += 0.1"` listener, the parent will receive the event and update the value of `postFontSize`.
223225

224226
<common-codepen-snippet title="Component basics: emitting events" slug="KKpGyrp" tab="html,result" :preview="false" />
225227

226-
We can list emitted events in the component's `emits` option.
228+
We can list emitted events in the component's `emits` option:
227229

228230
```js
229231
app.component('blog-post', {
@@ -232,11 +234,11 @@ app.component('blog-post', {
232234
})
233235
```
234236

235-
This will allow you to check all the events component emits and optionally [validate them](component-custom-events.html#validate-emitted-events)
237+
This will allow you to check all the events that a component emits and optionally [validate them](component-custom-events.html#validate-emitted-events).
236238

237239
### Emitting a Value With an Event
238240

239-
It's sometimes useful to emit a specific value with an event. For example, we may want the `<blog-post>` component to be in charge of how much to enlarge the text by. In those cases, we can use `$emit`'s 2nd parameter to provide this value:
241+
It's sometimes useful to emit a specific value with an event. For example, we may want the `<blog-post>` component to be in charge of how much to enlarge the text by. In those cases, we can pass a second parameter to `$emit` to provide this value:
240242

241243
```html
242244
<button @click="$emit('enlarge-text', 0.1)">
@@ -290,12 +292,12 @@ When used on a component, `v-model` instead does this:
290292
```
291293

292294
::: warning
293-
Please note we used `model-value` with kebab-case here because we are working with in-DOM template. You can find a detailed explanation on kebab-cased vs camelCased attributes in the [DOM Template Parsing Caveats](#dom-template-parsing-caveats) section
295+
Please note we used `model-value` with kebab-case here because we are working with in-DOM templates. You can find a detailed explanation on kebab-cased vs camelCased attributes in the [DOM Template Parsing Caveats](#dom-template-parsing-caveats) section
294296
:::
295297

296298
For this to actually work though, the `<input>` inside the component must:
297299

298-
- Bind the `value` attribute to a `modelValue` prop
300+
- Bind the `value` attribute to the `modelValue` prop
299301
- On `input`, emit an `update:modelValue` event with the new value
300302

301303
Here's that in action:
@@ -319,11 +321,7 @@ Now `v-model` should work perfectly with this component:
319321
<custom-input v-model="searchText"></custom-input>
320322
```
321323

322-
Another way of creating the `v-model` capability within a custom component is to use the ability of `computed` properties' to define a getter and setter.
323-
324-
In the following example, we refactor the `custom-input` component using a computed property.
325-
326-
Keep in mind, the `get` method should return the `modelValue` property, or whichever property is being using for binding, and the `set` method should fire off the corresponding `$emit` for that property.
324+
Another way of implementing `v-model` within this component is to use the ability of `computed` properties to define a getter and setter. The `get` method should return the `modelValue` property and the `set` method should emit the corresponding event:
327325

328326
```js
329327
app.component('custom-input', {
@@ -361,7 +359,7 @@ Which might render something like:
361359

362360
<common-codepen-snippet title="Component basics: slots" slug="jOPeaob" :preview="false" />
363361

364-
Fortunately, this task is made very simple by Vue's custom `<slot>` element:
362+
This can be achieved using Vue's custom `<slot>` element:
365363

366364
```js
367365
app.component('alert-box', {
@@ -374,7 +372,7 @@ app.component('alert-box', {
374372
})
375373
```
376374

377-
As you'll see above, we just add the slot where we want it to go -- and that's it. We're done!
375+
As you'll see above, we use the `<slot>` as a placeholder where we want the content to go and that's it. We're done!
378376

379377
That's all you need to know about slots for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Slots](component-slots.md).
380378

@@ -384,7 +382,7 @@ Sometimes, it's useful to dynamically switch between components, like in a tabbe
384382

385383
<common-codepen-snippet title="Component basics: dynamic components" slug="oNXaoKy" :preview="false" />
386384

387-
The above is made possible by Vue's `<component>` element with the `is` special attribute:
385+
The above is made possible by Vue's `<component>` element with the special `is` attribute:
388386

389387
```html
390388
<!-- Component changes when currentTabComponent changes -->
@@ -398,7 +396,7 @@ In the example above, `currentTabComponent` can contain either:
398396

399397
See [this sandbox](https://codepen.io/team/Vue/pen/oNXaoKy) to experiment with the full code, or [this version](https://codepen.io/team/Vue/pen/oNXapXM) for an example binding to a component's options object, instead of its registered name.
400398

401-
Keep in mind that this attribute can be used with regular HTML elements, however they will be treated as components, which means all attributes **will be bound as DOM attributes**. For some properties such as `value` to work as you would expect, you will need to bind them using the [`.prop` modifier](../api/directives.html#v-bind).
399+
You can also use the `is` attribute to create regular HTML elements.
402400

403401
That's all you need to know about dynamic components for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Dynamic & Async Components](./component-dynamic-async.html).
404402

@@ -414,7 +412,7 @@ This will lead to issues when using components with elements that have such rest
414412
</table>
415413
```
416414

417-
The custom component `<blog-post-row>` will be hoisted out as invalid content, causing errors in the eventual rendered output. Fortunately, we can use `v-is` special directive as a workaround:
415+
The custom component `<blog-post-row>` will be hoisted out as invalid content, causing errors in the eventual rendered output. We can use the special `v-is` directive as a workaround:
418416

419417
```html
420418
<table>
@@ -423,7 +421,7 @@ The custom component `<blog-post-row>` will be hoisted out as invalid content, c
423421
```
424422

425423
:::warning
426-
`v-is` value should be a JavaScript string literal:
424+
The `v-is` value is treated as a JavaScript expression, so we need to wrap the component name in quotes:
427425

428426
```html
429427
<!-- Incorrect, nothing will be rendered -->

0 commit comments

Comments
 (0)