Components directory

The components/ directory is where you put all your Vue components which can then be imported inside your pages or other components (learn more).

Nuxt automatically imports any components in your components/ directory (along with components that are registered by any modules you may be using).

  1. | components/
  2. --| TheHeader.vue
  3. --| TheFooter.vue

layouts/default.vue

  1. <template>
  2. <div>
  3. <TheHeader />
  4. <slot />
  5. <TheFooter />
  6. </div>
  7. </template>

Component Names

If you have a component in nested directories such as:

  1. | components/
  2. --| base/
  3. ----| foo/
  4. ------| Button.vue

… then the component’s name will be based on its own path directory and filename, with duplicate segments being removed. Therefore, the component’s name will be:

  1. <BaseFooButton />

For clarity, we recommend that the component’s filename matches its name. (So, in the example above, you could rename Button.vue to be BaseFooButton.vue.)

Dynamic components

If you want to use the Vue <component :is="someComputedComponent"> syntax, then you will need to use the resolveComponent helper provided by Vue.

For example:

  1. <template>
  2. <component :is="clickable ? MyButton : 'div'" />
  3. </template>
  4. <script setup>
  5. const MyButton = resolveComponent('MyButton')
  6. </script>

Alternatively, though not recommended, you can register all your components globally, which will create async chunks for all your components and make them available throughout your application.

  1. import { defineNuxtConfig } from 'nuxt3'
  2. export default defineNuxtConfig({
  3. components: {
  4. + global: true,
  5. + dirs: ['~/components']
  6. },
  7. })

The global option can also be set per component directory.

Dynamic Imports

To dynamically import a component (also known as lazy-loading a component) all you need to do is add the Lazy prefix to the component’s name.

layouts/default.vue

  1. <template>
  2. <div>
  3. <TheHeader />
  4. <slot />
  5. <LazyTheFooter />
  6. </div>
  7. </template>

This is particularly useful if the component is not always needed. By using the Lazy prefix you can delay loading the component code until the right moment, which can be helpful for optimizing your JavaScript bundle size.

pages/index.vue

  1. <template>
  2. <div>
  3. <h1>Mountains</h1>
  4. <LazyMountainsList v-if="show" />
  5. <button v-if="!show" @click="show = true">Show List</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. show: false
  13. }
  14. }
  15. }
  16. </script>

Direct imports

You can also explicitly import components from #components if you want or need to bypass Nuxt’s auto-importing functionality.

pages/index.vue

  1. <template>
  2. <div>
  3. <h1>Mountains</h1>
  4. <LazyMountainsList v-if="show" />
  5. <button v-if="!show" @click="show = true">Show List</button>
  6. <NuxtLink to="/">Home</NuxtLink>
  7. </div>
  8. </template>
  9. <script setup>
  10. import { NuxtLink, LazyMountainsList } from '#components'
  11. const show = ref(false)
  12. </script>

Component

Nuxt provides the <ClientOnly> component for purposely rendering a component only on client side. To import a component only on the client, register the component in a client-side only plugin.

pages/example.vue

  1. <template>
  2. <div>
  3. <Sidebar />
  4. <ClientOnly>
  5. <!-- this component will only be rendered on client-side -->
  6. <Comments />
  7. </ClientOnly>
  8. </div>
  9. </template>

Use a slot as fallback until <ClientOnly> is mounted on client side.

pages/example.vue

  1. <template>
  2. <div>
  3. <Sidebar />
  4. <ClientOnly>
  5. <!-- this component will only be rendered on client side -->
  6. <Comments />
  7. <template #fallback>
  8. <!-- this will be rendered on server side -->
  9. <p>Loading comments...</p>
  10. </template>
  11. </ClientOnly>
  12. </div>
  13. </template>

Library Authors

Making Vue component libraries with automatic tree-shaking and component registration is super easy ✨

You can use the components:dirs hook to extend the directory list without requiring user configuration in your Nuxt module.

Imagine a directory structure like this:

  1. | node_modules/
  2. ---| awesome-ui/
  3. ------| components/
  4. ---------| Alert.vue
  5. ---------| Button.vue
  6. ------| nuxt.js
  7. | pages/
  8. ---| index.vue
  9. | nuxt.config.js

Then in awesome-ui/nuxt.js you can use the components:dirs hook:

  1. import { join } from 'node:path'
  2. import { defineNuxtModule } from '@nuxt/kit'
  3. export default defineNuxtModule({
  4. hooks: {
  5. 'components:dirs'(dirs) {
  6. // Add ./components dir to the list
  7. dirs.push({
  8. path: join(__dirname, 'components'),
  9. prefix: 'awesome'
  10. })
  11. }
  12. }
  13. })

That’s it! Now in your project, you can import your UI library as a Nuxt module in your nuxt.config file:

  1. export default {
  2. modules: ['awesome-ui/nuxt']
  3. }

… and directly use the module components (prefixed with awesome-) in our pages/index.vue:

  1. <template>
  2. <div>
  3. My <AwesomeButton>UI button</AwesomeButton>!
  4. <awesome-alert>Here's an alert!</awesome-alert>
  5. </div>
  6. </template>

It will automatically import the components only if used and also support HMR when updating your components in node_modules/awesome-ui/components/.

🔎

Read and edit a live example in Examples > Auto Imports > Components