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
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.
44
44
45
45
## Reusing Components
46
46
@@ -66,7 +66,7 @@ It's common for an app to be organized into a tree of nested components:
66
66
67
67
For example, you might have components for a header, sidebar, and content area, each typically containing other components for navigation links, blog posts, etc.
68
68
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:
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.
80
80
81
81
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).
82
82
83
83
## Passing Data to Child Components with Props
84
84
85
85
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.
86
86
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:
88
88
89
89
```js
90
90
constapp=Vue.createApp({})
@@ -97,7 +97,9 @@ app.component('blog-post', {
97
97
app.mount('#blog-post-demo')
98
98
```
99
99
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.
101
103
102
104
Once a prop is registered, you can pass data to it as a custom attribute, like this:
103
105
@@ -175,7 +177,7 @@ Which can be used in the template to control the font size of all blog posts:
<blog-postv-for="post in posts":key="post.id":title="post.title"></blog-post>
180
182
</div>
181
183
</div>
@@ -205,7 +207,7 @@ The problem is, this button doesn't do anything:
205
207
</button>
206
208
```
207
209
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:
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).
236
238
237
239
### Emitting a Value With an Event
238
240
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:
240
242
241
243
```html
242
244
<button@click="$emit('enlarge-text', 0.1)">
@@ -290,12 +292,12 @@ When used on a component, `v-model` instead does this:
290
292
```
291
293
292
294
::: 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
294
296
:::
295
297
296
298
For this to actually work though, the `<input>` inside the component must:
297
299
298
-
- Bind the `value` attribute to a`modelValue` prop
300
+
- Bind the `value` attribute to the`modelValue` prop
299
301
- On `input`, emit an `update:modelValue` event with the new value
300
302
301
303
Here's that in action:
@@ -319,11 +321,7 @@ Now `v-model` should work perfectly with this component:
319
321
<custom-inputv-model="searchText"></custom-input>
320
322
```
321
323
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:
327
325
328
326
```js
329
327
app.component('custom-input', {
@@ -361,7 +359,7 @@ Which might render something like:
Fortunately, this task is made very simple by Vue's custom `<slot>` element:
362
+
This can be achieved using Vue's custom `<slot>` element:
365
363
366
364
```js
367
365
app.component('alert-box', {
@@ -374,7 +372,7 @@ app.component('alert-box', {
374
372
})
375
373
```
376
374
377
-
As you'll see above, we just add the slotwhere 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!
378
376
379
377
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).
380
378
@@ -384,7 +382,7 @@ Sometimes, it's useful to dynamically switch between components, like in a tabbe
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:
388
386
389
387
```html
390
388
<!-- Component changes when currentTabComponent changes -->
@@ -398,7 +396,7 @@ In the example above, `currentTabComponent` can contain either:
398
396
399
397
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.
400
398
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.
402
400
403
401
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).
404
402
@@ -414,7 +412,7 @@ This will lead to issues when using components with elements that have such rest
414
412
</table>
415
413
```
416
414
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:
418
416
419
417
```html
420
418
<table>
@@ -423,7 +421,7 @@ The custom component `<blog-post-row>` will be hoisted out as invalid content, c
423
421
```
424
422
425
423
:::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:
0 commit comments