<?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: ryosh</title>
    <description>The latest articles on DEV Community by ryosh (@ryosh).</description>
    <link>https://dev.to/ryosh</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%2F393664%2F935534c3-4fc8-4d3a-9f5e-d9ba5c588849.png</url>
      <title>DEV Community: ryosh</title>
      <link>https://dev.to/ryosh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ryosh"/>
    <language>en</language>
    <item>
      <title>Use @nuxt/components to automatically import components!</title>
      <dc:creator>ryosh</dc:creator>
      <pubDate>Sat, 23 May 2020 17:02:55 +0000</pubDate>
      <link>https://dev.to/ryosh/use-nuxt-components-to-automatically-import-components-51cf</link>
      <guid>https://dev.to/ryosh/use-nuxt-components-to-automatically-import-components-51cf</guid>
      <description>&lt;h2&gt;
  
  
  What's @nuxt/components
&lt;/h2&gt;

&lt;p&gt;This is a module that automatically imports components in Nuxt.js development.&lt;br&gt;
You can omit the definition of the &lt;code&gt;import&lt;/code&gt; statement and the &lt;code&gt;components&lt;/code&gt; field.&lt;br&gt;
&lt;a href="https://github.com/nuxt/component"&gt;https://github.com/nuxt/component&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;Follow the USAGE in the README.&lt;br&gt;
&lt;a href="https://github.com/nuxt/components#usage"&gt;https://github.com/nuxt/components#usage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Normally, you need to import a component to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;ComponentFoo /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&amp;lt;script&amp;gt;
import ComponentFoo from '~/components/ComponentFoo.vue'

export default {
  components: {
    ComponentFoo
  }
}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but with @nuxt/components, you don't need to specify the import in &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;ComponentFoo /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  In the case of dynamic components
&lt;/h2&gt;

&lt;p&gt;Put &lt;code&gt;Lazy&lt;/code&gt; at the beginning of a component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;LazyComponentFoo v-if='foo' /&amp;gt;
    &amp;lt;button @click='loadFoo'&amp;gt;Load Foo&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data () {
    return {
      foo: null
    }
  },
  methods: {
    async loadFoo () {
      this.foo = await this.getFoo()
    },
    getFoo () {
      return new Promise(resolve =&amp;gt; {
        setTimeout(() =&amp;gt; {
          resolve('foo');
        }, 2000);
      });
    }
  }
}
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  In the case of a nested the same name component
&lt;/h2&gt;

&lt;p&gt;For example, if there is the same name component &lt;code&gt;Bar.vue&lt;/code&gt;in a different directory, such as the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;components/
  Bar.vue
  foo/
    Bar.vue
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following to &lt;code&gt;nuxt.config.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;components: {
    dirs: [
      '~/components/',
      {
        path: '~/components/foo/',
        prefix: 'foo'
      }
    ]
  },
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Components under &lt;code&gt;foo/&lt;/code&gt; are prefixed with &lt;code&gt;Foo&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    // components/Bar.vue
    &amp;lt;Bar /&amp;gt;
    // components/foo/Bar.vue
    &amp;lt;FooBar /&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/nuxt/components"&gt;https://github.com/nuxt/components&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nuxt</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
