Skip to content

Commit fdb6759

Browse files
committed
fine tune attrs
1 parent a703e43 commit fdb6759

File tree

3 files changed

+73
-51
lines changed

3 files changed

+73
-51
lines changed

src/guide/components/attrs.md

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,10 @@ aside: deep
66

77
> This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
88
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-
<div class="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-
export default {
33-
setup(props, ctx) {
34-
// fallthrough attributes are exposed as ctx.attrs
35-
}
36-
}
37-
```
38-
39-
</div>
40-
41-
<div class="options-api">
42-
43-
You can also access them in JavaScript via the `$attrs` instance property:
44-
45-
```js
46-
export default {
47-
created() {
48-
console.log(this.$attrs)
49-
}
50-
}
51-
```
52-
53-
</div>
54-
559
## Attribute Inheritance
5610

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+
5713
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:
5814

5915
```vue-html
@@ -90,14 +46,31 @@ Then the final rendered DOM would now become:
9046

9147
### `v-on` Listener Inheritance
9248

93-
Same rule applies to the event listeners:
49+
Same rule applies to `v-on` event listeners:
9450

9551
```vue-html
9652
<MyButton @click="onClick" />
9753
```
9854

9955
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.
10056

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+
10174
## Disabling Attribute Inheritance
10275

10376
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 {
12194

12295
</div>
12396

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+
```
125104

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.).
127106

128107
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:
129108

@@ -141,6 +120,8 @@ We want all fallthrough attributes like `class` and `v-on` listeners to be appli
141120
</div>
142121
```
143122

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+
144125
## Attribute Inheritance on Multiple Root Nodes
145126

146127
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:
164145
<main v-bind="$attrs">...</main>
165146
<footer>...</footer>
166147
```
148+
149+
## Accessing Fallthrough Attributes in JavaScript
150+
151+
<div class="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+
export default {
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+
<div class="options-api">
177+
178+
You can access a component's fallthrough attributes via the `$attrs` instance property:
179+
180+
```js
181+
export default {
182+
created() {
183+
console.log(this.$attrs)
184+
}
185+
}
186+
```
187+
188+
</div>

src/guide/components/events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
44
5-
## Event Names
5+
## Event Name Casing
66

77
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:
88

src/guide/components/props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ In the two examples above, we happen to pass string values, but _any_ type of va
205205

206206
### Binding Multiple Properties Using an Object
207207

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:
209209

210210
<div class="options-api">
211211

0 commit comments

Comments
 (0)