# Component Registration

This page assumes you've already read the Components Basics. Read that first if you are new to components.

# Component Names

When registering a component, it will always be given a name. For example, in the global registration we've seen so far:

const app = Vue.createApp({...})

app.component('my-component-name', {
  /* ... */
})
1
2
3
4
5

The component's name is the first argument of app.component. In the example above, the component's name is "my-component-name".

The name you give a component may depend on where you intend to use it. When using a component directly in the DOM (as opposed to in a string template or single-file component), we strongly recommend following the W3C rules for custom tag names:

  1. All lowercase
  2. Contains a hyphen (i.e., has multiple words connected with the hyphen symbol)

By doing so, this will help you avoid conflicts with current and future HTML elements.

You can see other recommendations for component names in the Style Guide.

# Name Casing

When defining components in a string template or a single-file component, you have two options when defining component names:

# With kebab-case

app.component('my-component-name', {
  /* ... */
})
1
2
3

When defining a component with kebab-case, you must also use kebab-case when referencing its custom element, such as in <my-component-name>.

# With PascalCase

app.component('MyComponentName', {
  /* ... */
})
1
2
3

When defining a component with PascalCase, you can use either case when referencing its custom element. That means both <my-component-name> and <MyComponentName> are acceptable. Note, however, that only kebab-case names are valid directly in the DOM (i.e. non-string templates).

# Global Registration

So far, we've only created components using app.component:

Vue.createApp({...}).component('my-component-name', {
  // ... options ...
})
1
2
3

These components are globally registered for the application. That means they can be used in the template of any component instance within this application:

const app = Vue.createApp({})

app.component('component-a', {
  /* ... */
})
app.component('component-b', {
  /* ... */
})
app.component('component-c', {
  /* ... */
})

app.mount('#app')
1
2
3
4
5
6
7
8
9
10
11
12
13
<div id="app">
  <component-a></component-a>
  <component-b></component-b>
  <component-c></component-c>
</div>
1
2
3
4
5

This even applies to all subcomponents, meaning all three of these components will also be available inside each other.

# Local Registration

Global registration often isn't ideal. For example, if you're using a build system like Webpack, globally registering all components means that even if you stop using a component, it could still be included in your final build. This unnecessarily increases the amount of JavaScript your users have to download.

In these cases, you can define your components as plain JavaScript objects:

const ComponentA = {
  /* ... */
}
const ComponentB = {
  /* ... */
}
const ComponentC = {
  /* ... */
}
1
2
3
4
5
6
7
8
9

Then define the components you'd like to use in a components option:

const app = Vue.createApp({
  components: {
    'component-a': ComponentA,
    'component-b': ComponentB
  }
})
1
2
3
4
5
6

For each property in the components object, the key will be the name of the custom element, while the value will contain the options object for the component.

Note that locally registered components are not also available in subcomponents. For example, if you wanted ComponentA to be available in ComponentB, you'd have to use:

const ComponentA = {
  /* ... */
}

const ComponentB = {
  components: {
    'component-a': ComponentA
  }
  // ...
}
1
2
3
4
5
6
7
8
9
10

Or if you're using ES2015 modules, such as through Babel and Webpack, that might look more like:

import ComponentA from './ComponentA.vue'

export default {
  components: {
    ComponentA
  }
  // ...
}
1
2
3
4
5
6
7
8

Note that in ES2015+, placing a variable name like ComponentA inside an object is shorthand for ComponentA: ComponentA, meaning the name of the variable is both:

  • the custom element name to use in the template, and
  • the name of the variable containing the component options

# Module Systems

If you're not using a module system with import/require, you can probably skip this section for now. If you are, we have some special instructions and tips just for you.

# Local Registration in a Module System

If you're still here, then it's likely you're using a module system, such as with Babel and Webpack. In these cases, we recommend creating a components directory, with each component in its own file.

Then you'll need to import each component you'd like to use, before you locally register it. For example, in a hypothetical ComponentB.js or ComponentB.vue file:

import ComponentA from './ComponentA'
import ComponentC from './ComponentC'

export default {
  components: {
    ComponentA,
    ComponentC
  }
  // ...
}
1
2
3
4
5
6
7
8
9
10

Now both ComponentA and ComponentC can be used inside ComponentB's template.

Deployed on Netlify.
Last updated: 9/30/2020, 5:58:04 AM