Skip to main content

Components

Nuxt Kit provides a set of utilities to help you work with components. You can register components globally or locally, and also add directories to be scanned for components. Components are the building blocks of your Nuxt application. They are reusable Vue instances that can be used to create a user interface.

addComponentsDir

Register a directory to be scanned for components and imported only when used. Keep in mind, that this does not register components globally, until you specify global: true option.

Type

function addComponentsDir(dir: ComponentsDir, opts: { prepend?: boolean } = {}): void

Parameters

dir: An object with the following properties:
PropertyTypeRequiredDescription
pathstringtruePath (absolute or relative) to the directory containing your components.
patternstring | string[]falseAccept Pattern that will be run against specified path.
ignorestring[]falseIgnore patterns that will be run against specified path.
prefixstringfalsePrefix all matched components with this string.
pathPrefixbooleanfalsePrefix component name by its path.
globalbooleanfalseIf enabled, registers components to be globally available.
islandbooleanfalseIf enabled, registers components as islands.
watchbooleanfalseWatch specified path for changes, including file additions and file deletions.
extensionsstring[]falseExtensions supported by Nuxt builder.
transpile'auto' | booleanfalseTranspile specified path using build.transpile. If set to 'auto', it will set transpile: true if node_modules/ is in path.
opts:
PropertyTypeRequiredDescription
prependbooleanfalseIf set to true, the directory will be prepended to the array with unshift() instead of push().

Usage

import { defineNuxtModule, addComponentsDir, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: '@nuxt/ui',
    configKey: 'ui',
  },
  setup() {
    const resolver = createResolver(import.meta.url)
    
    addComponentsDir({
      path: resolver.resolve('./runtime/components'),
      prefix: 'U',
      pathPrefix: false,
    })
  },
})

addComponent

Register a component to be automatically imported.

Type

function addComponent(options: AddComponentOptions): void

Parameters

options: An object with the following properties:
PropertyTypeRequiredDescription
namestringtrueComponent name.
filePathstringtruePath to the component.
declarationPathstringfalsePath to component’s declaration file. Used to generate components’ type templates.
pascalNamestringfalsePascal case component name. If not provided, generated from the component name.
kebabNamestringfalseKebab case component name. If not provided, generated from the component name.
exportstringfalseSpecify named or default export. If not provided, set to 'default'.
globalbooleanfalseIf enabled, registers component to be globally available.
islandbooleanfalseIf enabled, registers component as island.
mode'client' | 'server' | 'all'falseIndicates if component should render on client, server or both. By default, renders on both.
prioritynumberfalsePriority of the component. If multiple components have the same name, the one with the highest priority will be used.

Usage

import { addComponent, createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: '@nuxt/image',
    configKey: 'image',
  },
  setup() {
    const resolver = createResolver(import.meta.url)

    addComponent({
      name: 'NuxtImg',
      filePath: resolver.resolve('./runtime/components/NuxtImg.vue'),
    })

    addComponent({
      name: 'NuxtPicture',
      filePath: resolver.resolve('./runtime/components/NuxtPicture.vue'),
    })
  },
})

Auto-importing from npm packages

If you want to auto-import a component from an npm package, and the component is a named export (rather than the default), you can use the export option.
import { addComponent, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  setup() {
    // import { MyComponent as MyAutoImportedComponent } from 'my-npm-package'
    addComponent({
      name: 'MyAutoImportedComponent',
      export: 'MyComponent',
      filePath: 'my-npm-package',
    })
  },
})

Source

View source on GitHub