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/guide/components/attrs.md
+71-49Lines changed: 71 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -6,54 +6,10 @@ aside: deep
6
6
7
7
> This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
8
8
9
-
A component non-prop attribute is an attribute or event listener that is passed to a component, but is not explicitly declared in [props](./props) or [emits](./events.html#defining-custom-events). Common examples of this include `class`, `style`, and `id` attributes.
10
-
11
-
These fallthrough attributes can be accessed directly in template expressions as `$attrs`:
12
-
13
-
```vue-html
14
-
<span>Fallthrough attributes: {{ $attrs }}</span>
15
-
```
16
-
17
-
<divclass="composition-api">
18
-
19
-
You can also access them in `<script setup>` using the `useAttrs()` API:
20
-
21
-
```vue
22
-
<script setup>
23
-
import { useAttrs } from 'vue'
24
-
25
-
const attrs = useAttrs()
26
-
</script>
27
-
```
28
-
29
-
If not using `<script setup>`, `attrs` will be exposed as a property of the `setup()` context:
30
-
31
-
```js
32
-
exportdefault {
33
-
setup(props, ctx) {
34
-
// fallthrough attributes are exposed as ctx.attrs
35
-
}
36
-
}
37
-
```
38
-
39
-
</div>
40
-
41
-
<divclass="options-api">
42
-
43
-
You can also access them in JavaScript via the `$attrs` instance property:
44
-
45
-
```js
46
-
exportdefault {
47
-
created() {
48
-
console.log(this.$attrs)
49
-
}
50
-
}
51
-
```
52
-
53
-
</div>
54
-
55
9
## Attribute Inheritance
56
10
11
+
A "fallthrough attribute" is an attribute or `v-on` event listener that is passed to a component, but is not explicitly declared in the receiving component's [props](./props) or [emits](./events.html#defining-custom-events). Common examples of this include `class`, `style`, and `id` attributes.
12
+
57
13
When a component renders a single root element, fallthrough attributes will be automatically added to the root element's attributes. For example, given a `<MyButton>` component with the following template:
58
14
59
15
```vue-html
@@ -90,14 +46,31 @@ Then the final rendered DOM would now become:
90
46
91
47
### `v-on` Listener Inheritance
92
48
93
-
Same rule applies to the event listeners:
49
+
Same rule applies to `v-on` event listeners:
94
50
95
51
```vue-html
96
52
<MyButton @click="onClick" />
97
53
```
98
54
99
55
The `click` listener will be added to the root element of `<MyButton>`, i.e. the native `<button>` element. When the native `<button>` is clicked, it will trigger the `onClick` method of the parent component. If the native `<button>` already has a `click` listener bound with `v-on`, then both listeners will trigger.
100
56
57
+
### Nested Component Inheritance
58
+
59
+
If a component renders another component as its root node, for example, we refactored `<MyButton>` to render a `<BaseButton>` as its root:
60
+
61
+
```vue-html
62
+
<!-- template of <MyButton/> that simply renders another component -->
63
+
<BaseButton />
64
+
```
65
+
66
+
Then the fallthrough attributes received by `<MyButton>` will be automatically forwarded to `<BaseButton>`.
67
+
68
+
Note that:
69
+
70
+
1. Forwarded attributes do not include any attributes that are declared as props by `<MyButton>` - in other words, the declared props have been "consumed" by `<MyButton>`.
71
+
72
+
2. Forwarded attributes may be accepted as props by `<BaseButton>`, if declared by it.
73
+
101
74
## Disabling Attribute Inheritance
102
75
103
76
If you do **not** want a component to automatically inherit attributes, you can set `inheritAttrs: false` in the component's options.
@@ -121,9 +94,15 @@ export default {
121
94
122
95
</div>
123
96
124
-
The common scenario for disabling an attribute inheritance is when attributes need to be applied to other elements besides the root node.
97
+
The common scenario for disabling an attribute inheritance is when attributes need to be applied to other elements besides the root node. By setting the `inheritAttrs` option to `false`, you can take full control over where the fallthrough attributes should be applied to.
98
+
99
+
These fallthrough attributes can be accessed directly in template expressions as `$attrs`:
100
+
101
+
```vue-html
102
+
<span>Fallthrough attributes: {{ $attrs }}</span>
103
+
```
125
104
126
-
By setting the `inheritAttrs` option to `false`, you can take full control over where the fallthrough attributes should be applied to. The `$attrs` object includes all attributes not included to component `props` and `emits` properties (e.g., `class`, `style`, `v-on` listeners, etc.).
105
+
The `$attrs` object includes all attributes not included to component `props` and `emits` properties (e.g., `class`, `style`, `v-on` listeners, etc.).
127
106
128
107
Using our `<MyButton>` component example from the [previous section](#attribute-inheritance) - sometimes we may need to wrap the actual `<button>` element with an extra `<div>` for styling purposes:
129
108
@@ -141,6 +120,8 @@ We want all fallthrough attributes like `class` and `v-on` listeners to be appli
141
120
</div>
142
121
```
143
122
123
+
Remember that [`v-bind` without argument](/guide/essentials/template-syntax.html#dynamically-binding-multiple-attributes) binds every property of an object as attributes to the target element.
124
+
144
125
## Attribute Inheritance on Multiple Root Nodes
145
126
146
127
Unlike single root node components, components with multiple root nodes do not have an automatic attribute fallthrough behavior. If `$attrs` are not bound explicitly, a runtime warning will be issued.
@@ -164,3 +145,44 @@ The warning will be suppressed if `$attrs` is explicitly bound:
164
145
<main v-bind="$attrs">...</main>
165
146
<footer>...</footer>
166
147
```
148
+
149
+
## Accessing Fallthrough Attributes in JavaScript
150
+
151
+
<divclass="composition-api">
152
+
153
+
You can access a component's fallthrough attributes in `<script setup>` using the `useAttrs()` API:
154
+
155
+
```vue
156
+
<script setup>
157
+
import { useAttrs } from 'vue'
158
+
159
+
const attrs = useAttrs()
160
+
</script>
161
+
```
162
+
163
+
If not using `<script setup>`, `attrs` will be exposed as a property of the `setup()` context:
164
+
165
+
```js
166
+
exportdefault {
167
+
setup(props, ctx) {
168
+
// fallthrough attributes are exposed as ctx.attrs
169
+
console.log(ctx.attrs)
170
+
}
171
+
}
172
+
```
173
+
174
+
</div>
175
+
176
+
<divclass="options-api">
177
+
178
+
You can access a component's fallthrough attributes via the `$attrs` instance property:
Copy file name to clipboardExpand all lines: src/guide/components/events.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
> This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
4
4
5
-
## Event Names
5
+
## Event Name Casing
6
6
7
7
Like components and props, event names provide an automatic case transformation. If you emit an event from the child component in camel case, you will be able to add a kebab-cased listener in the parent:
Copy file name to clipboardExpand all lines: src/guide/components/props.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -205,7 +205,7 @@ In the two examples above, we happen to pass string values, but _any_ type of va
205
205
206
206
### Binding Multiple Properties Using an Object
207
207
208
-
If you want to pass all the properties of an object as props, you can use `v-bind` without an argument (`v-bind` instead of `:prop-name`). For example, given a `post` object:
208
+
If you want to pass all the properties of an object as props, you can use [`v-bind` without an argument](/guide/essentials/template-syntax.html#dynamically-binding-multiple-attributes) (`v-bind` instead of `:prop-name`). For example, given a `post` object:
0 commit comments