Overview
Vue components need to be registered so they can be discovered when rendering templates. There are two types of component registration: global and local.Global Registration
Globally registered components can be used in any template within the application.app.component() method can be chained:
Usage in Templates
Downsides of Global Registration
- Tree-shaking limitations: Globally registered components cannot be removed by build tools even if unused
- Implicit dependencies: In large applications, it makes dependency relationships less explicit
- Namespace pollution: All components share the same global namespace
Local Registration
Local registration limits component availability to the current scope, making dependencies more explicit and improving tree-shaking.With <script setup>
Imported components are automatically available locally:
Without <script setup>
Use the components option:
Component Name Casing
PascalCase (Recommended)
- Differentiates Vue components from native HTML elements
- Consistent with JavaScript class naming conventions
- Better IDE autocomplete support
kebab-case
Registration with TypeScript
Typing Global Components
Extend theGlobalComponents interface for type safety:
runtime-core/src/component.ts:174-179
Typing Local Components
defineComponent
ThedefineComponent() helper provides type inference for component definitions:
Function Signature
runtime-core/src/apiDefineComponent.ts:305-315
Setup Function Overload
runtime-core/src/apiDefineComponent.ts:147-180
Implementation Details
ThedefineComponent implementation is close to a no-op, primarily providing type inference:
runtime-core/src/apiDefineComponent.ts:305-315
Best Practices
- Prefer local registration for better tree-shaking and explicit dependencies
- Use PascalCase for component names in single-file components
- Leverage
<script setup>for automatic local registration - Type global components when using TypeScript
- Use
defineComponentwhen not using<script setup>for better type inference
Related APIs
- Props - Learn about component props
- Events - Learn about component events
- Async Components - Learn about lazy loading components