<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: emile2636</title>
    <description>The latest articles on DEV Community by emile2636 (@emile2636).</description>
    <link>https://dev.to/emile2636</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F264479%2F57703d71-3188-44f3-936f-5105217b31d3.png</url>
      <title>DEV Community: emile2636</title>
      <link>https://dev.to/emile2636</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emile2636"/>
    <language>en</language>
    <item>
      <title>Create components with native HTML tags’ attributes by using “inheritAttrs” in Vue 2.4.0+</title>
      <dc:creator>emile2636</dc:creator>
      <pubDate>Mon, 04 Nov 2019 08:32:37 +0000</pubDate>
      <link>https://dev.to/emile2636/create-components-with-native-html-tags-attributes-by-using-inheritattrs-in-vue-2-4-0-48jb</link>
      <guid>https://dev.to/emile2636/create-components-with-native-html-tags-attributes-by-using-inheritattrs-in-vue-2-4-0-48jb</guid>
      <description>&lt;h1&gt;
  
  
  Scenario
&lt;/h1&gt;

&lt;p&gt;When we want to pass some custom value to other components in Vue, passing “&lt;code&gt;props&lt;/code&gt;” would be the first chose. However, when it comes to common attributes in HTML tags like &lt;code&gt;autocomplete&lt;/code&gt;, &lt;code&gt;maxlength&lt;/code&gt;, etc. We also want a “native HTML like” develop experience.&lt;/p&gt;

&lt;p&gt;For example, we create a &lt;code&gt;CustomInput&lt;/code&gt; component which contains &lt;code&gt;input&lt;/code&gt; tag wrapped by a &lt;code&gt;div&lt;/code&gt; with a class for styling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// my custom input CustomInput
&amp;lt;template&amp;gt;
    &amp;lt;div class="myClass"&amp;gt;
        &amp;lt;input attributes /&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

// parent component import CustomInput 
&amp;lt;template&amp;gt;
    &amp;lt;div class="wrapper"&amp;gt;
        ...
        &amp;lt;custom-input maxlength="5" autocomplete="hello" /&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we want to do is that passing down attributes from parent component to &lt;code&gt;CustomInput&lt;/code&gt; without using &lt;code&gt;props&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Practice
&lt;/h1&gt;

&lt;p&gt;By default, when attributes are passed down to a child component, the root HTML tag in child component will inherit those attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// the output HTML from the above example
&amp;lt;div class="wrapper"&amp;gt;
    &amp;lt;div class="myClass" maxlength="5" autocomplete="hello"&amp;gt;
        &amp;lt;input /&amp;gt;
    &amp;lt;div/&amp;gt;
&amp;lt;div/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, what we expected is that attributes can be inherited by &lt;code&gt;&amp;lt;input/&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Since Vue 2.4.0, there’s an option called “&lt;code&gt;inheritAttrs&lt;/code&gt;”. By setting this option &lt;code&gt;inheritAttrs: false&lt;/code&gt;, the default action will not be executed. &lt;/p&gt;

&lt;p&gt;Finaly, we can use &lt;code&gt;v-bind&lt;/code&gt; &amp;amp; &lt;code&gt;$attrs&lt;/code&gt; to setup the attributes manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
export default {
    inheritAttrs: false,
    data(){
        return {
            myLength:0,
        }
    },
    created(){
        this.myLength = this.$attrs.maxlength
    }
}
&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;div class="myClass"&amp;gt;
        &amp;lt;input v-bind="{...$attrs, maxlength:undefined}" /&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Besides of “Destructuring Assignment” the &lt;code&gt;$attr&lt;/code&gt; object and attaching it by using &lt;code&gt;v-bind&lt;/code&gt; , we can also replace the attribute’s value to &lt;code&gt;undefined&lt;/code&gt; and manipulate it by ourself in &lt;code&gt;data&lt;/code&gt; option for other usage.&lt;/p&gt;




&lt;p&gt;It’s only a little trick for building custom components. Hope you are enjoy the article, and keep following me for the next sharing.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>html</category>
      <category>attributes</category>
    </item>
  </channel>
</rss>
