|
| 1 | +# Non-Prop Attributes |
| 2 | + |
| 3 | +> This page assumes you've already read the [Components Basics](component-basics.md). Read that first if you are new to components. |
| 4 | +
|
| 5 | +A component non-prop attribute is an attribute or event listener that is passed to a component, but does not have a corresponding property defined in [props](component-props) or [emits](component-custom-events.html#defining-custom-events). Common examples of this include `class`, `style`, and `id` attributes. |
| 6 | + |
| 7 | +## Attribute Inheritance |
| 8 | + |
| 9 | +When a component returns a single root node, non-prop attributes will automatically be added to the root node's attributes. For example, in the instance of a date-picker component: |
| 10 | + |
| 11 | +```js |
| 12 | +app.component('date-picker', { |
| 13 | + template: ` |
| 14 | + <div class="date-picker"> |
| 15 | + <input type="datetime" /> |
| 16 | + </div> |
| 17 | + ` |
| 18 | +}) |
| 19 | +``` |
| 20 | + |
| 21 | +In the event we need to define the status of the date-picker component via a `data-status` property, it will be applied to the root node (i.e., `div.date-picker`). |
| 22 | + |
| 23 | +```html |
| 24 | +<!-- Date-picker component with a non-prop attribute --> |
| 25 | +<date-picker data-status="activated"></date-picker> |
| 26 | + |
| 27 | +<!-- Rendered date-picker component --> |
| 28 | +<div class="date-picker" data-status="activated"> |
| 29 | + <input type="datetime" /> |
| 30 | +</div> |
| 31 | +``` |
| 32 | + |
| 33 | +Same rule applies to the event listeners: |
| 34 | + |
| 35 | +```html |
| 36 | +<date-picker @change="submitChange"></date-picker> |
| 37 | +``` |
| 38 | + |
| 39 | +```js |
| 40 | +app.component('date-picker', { |
| 41 | + created() { |
| 42 | + console.log(this.$attrs) // { onChange: () => {} } |
| 43 | + } |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +This might be helpful when we have an HTML element with `change` event as a root element of `date-picker`. |
| 48 | + |
| 49 | +```js |
| 50 | +app.component('date-picker', { |
| 51 | + template: ` |
| 52 | + <select> |
| 53 | + <option value="1">Yesterday</option> |
| 54 | + <option value="2">Today</option> |
| 55 | + <option value="3">Tomorrow</option> |
| 56 | + </select> |
| 57 | + ` |
| 58 | +}) |
| 59 | +``` |
| 60 | + |
| 61 | +In this case, `change` event listener is passed from the parent component to the child and it will be triggered on native `<select>` `change` event. We won't need to emit an event from the `date-picker` explicitly: |
| 62 | + |
| 63 | +```html |
| 64 | +<div id="date-picker" class="demo"> |
| 65 | + <date-picker @change="showChange"></date-picker> |
| 66 | +</div> |
| 67 | +``` |
| 68 | + |
| 69 | +```js |
| 70 | +const app = Vue.createApp({ |
| 71 | + methods: { |
| 72 | + showChange(event) { |
| 73 | + console.log(event.target.value) // will log a value of the selected option |
| 74 | + } |
| 75 | + } |
| 76 | +}) |
| 77 | +``` |
| 78 | + |
| 79 | +## Disabling Attribute Inheritance |
| 80 | + |
| 81 | +If you do **not** want a component to automatically inherit attributes, you can set `inheritAttrs: false` in the component's options. |
| 82 | + |
| 83 | +The common scenario for disabling an attribute inheritance is when attributes need to be applied to other elements besides the root node. |
| 84 | + |
| 85 | +By setting the `inheritAttrs` option to `false`, this gives you access to the component's `$attrs` property, which includes all attributes not included to component `props` and `emits` properties (e.g., `class`, `style`, `v-on` listeners, etc.). |
| 86 | + |
| 87 | +Using our date-picker component example from the [previous section]('#attribute-inheritance), in the event we need to apply all non-prop attributes to the `input` element rather than the root `div` element, this can be accomplished by using the `v-bind` shortcut. |
| 88 | + |
| 89 | +```js{5} |
| 90 | +app.component('date-picker', { |
| 91 | + inheritAttrs: false, |
| 92 | + template: ` |
| 93 | + <div class="date-picker"> |
| 94 | + <input type="datetime" v-bind="$attrs" /> |
| 95 | + </div> |
| 96 | + ` |
| 97 | +}) |
| 98 | +``` |
| 99 | + |
| 100 | +With this new configuration, our `data-status` attribute will be applied to our `input` element! |
| 101 | + |
| 102 | +```html |
| 103 | +<!-- Date-picker component with a non-prop attribute --> |
| 104 | +<date-picker data-status="activated"></date-picker> |
| 105 | + |
| 106 | +<!-- Rendered date-picker component --> |
| 107 | +<div class="date-picker"> |
| 108 | + <input type="datetime" data-status="activated" /> |
| 109 | +</div> |
| 110 | +``` |
| 111 | + |
| 112 | +## Attribute Inheritance on Multiple Root Nodes |
| 113 | + |
| 114 | +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. |
| 115 | + |
| 116 | +```html |
| 117 | +<custom-layout id="custom-layout" @click="changeValue"></custom-layout> |
| 118 | +``` |
| 119 | + |
| 120 | +```js |
| 121 | +// This will raise a warning |
| 122 | +app.component('custom-layout', { |
| 123 | + template: ` |
| 124 | + <header>...</header> |
| 125 | + <main>...</main> |
| 126 | + <footer>...</footer> |
| 127 | + ` |
| 128 | +}) |
| 129 | + |
| 130 | +// No warnings, $attrs are passed to <main> element |
| 131 | +app.component('custom-layout', { |
| 132 | + template: ` |
| 133 | + <header>...</header> |
| 134 | + <main v-bind="$attrs">...</main> |
| 135 | + <footer>...</footer> |
| 136 | + ` |
| 137 | +}) |
| 138 | +``` |
0 commit comments