Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are inheritAttrs: false and $attrs used for in Vue?

As the question suggests, I can't figure out their meaning and why I should use it. It's said that it can be used so that when we have many components and we want to pass data from parent to the child's child's child's component, we don't have to use props. Is this true?

It'd be nice If you could provide an easier example. Vue.js docs don't mention much on it.

like image 589
Nika Kurashvili Avatar asked May 20 '19 15:05

Nika Kurashvili


People also ask

What are Attrs in Vue?

The definition of $attrs, varies between the two major versions of the framework, but in this article, we are going to mainly cover Vue 3, where @attrs can be seen as: A component property that holds all of the attribute, property, and custom events that are not currently defined within the component.

Why We Use $V in VUE JS?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue. js 2.0), which is intended to check every input, which is made in a non-html form.

What are props used for in Vue?

“Props” is a special keyword which stands for properties. It can be registered on a component to pass data from a parent component to one of its children components. This is a lot easier compared to using state management libraries like vuex for Vue. js applications.

What is difference between props and data in Vue?

So what's the difference between props and data? Data is the private memory of each component where you can store any variables you need. Props are how you pass this data from a parent component down to a child component.

What is the use of $ATTRS in Vue?

If you are a Vue developer, $listeners can be used more casually. $listeners is used for passing the event to be invoked in a child component. As similar to $listeners, Setting v-bind="$attrs" in a parent component with props can be also used for passing data.

What is the use of attribute inheritance?

Have a look at the "Disabling Attribute Inheritance"section of the docs and the api descriptionfor the full details. It's main usage is to define so-called "transparent" components that pass-through attributes. The example given in the doc is a component wrapping an inputelement:

What is the difference between props and attributes in Vue?

Props are meant for actual data while attributes are meant to be used directly in the html of the template. – bernie May 20 '19 at 19:12 3 v-bind="$attrs" will tell vue that you wanna bind attrs supplied to the component to a given tag.

What is the use of pass-through attributes in a component?

It's main usage is to define so-called "transparent" components that pass-through attributes. The example given in the doc is a component wrapping an inputelement:


1 Answers

Have a look at the "Disabling Attribute Inheritance" section of the docs and the api description for the full details.

It's main usage is to define so-called "transparent" components that pass-through attributes. The example given in the doc is a component wrapping an input element:

// Component Vue.component('base-input', {   inheritAttrs: false,   props: ['label', 'value'],   template: `     <label>       {{ label }}       <input         v-bind="$attrs"         v-bind:value="value"         v-on:input="$emit('input', $event.target.value)"       >     </label>   ` })  // Usage <base-input   v-model="username"   required   placeholder="Enter your username" /> 

The required and placeholder attributes are then set on the input instead of the wrapping label.

It doesn't really have anything to do with children of children of components but it can be used in such a hierarchy.

I hope that clears things up for you.

like image 53
bernie Avatar answered Sep 20 '22 04:09

bernie