This is a globally registered component!
`
});
```
Once registered, you can use `Vue3 Api App Component
## Vue 3 Global API: app.component()
In Vue 3, the `app.component()` method is a fundamental global API used to register global components. Once a component is registered globally, it can be used anywhere within your application's templates without the need to import it individually in every file.
---
## 1. What is `app.component()`?
`app.component()` is an instance method of the Vue application instance (created via `createApp`). It serves two primary purposes:
1. **Register** a global component by providing both a name and a component definition.
2. **Retrieve** an already registered global component by passing only its name.
### Syntax
```javascript
// 1. Registration
app.component(name, definition);
// 2. Retrieval
const MyComponent = app.component(name);
```
### Parameters
* **`name`** (`string`): The name of the component. This is the tag name you will use inside your HTML or Vue templates (e.g., ``).
* **`definition`** (`Component` or `DefineComponent`): An object containing the component's options (such as `template`, `data`, `methods`, `setup`, etc.), or a compiled Single File Component (SFC).
### Basic Example
```javascript
import { createApp } from 'vue';
const app = createApp({});
// Register a simple global component
app.component('my-component', {
template: ` ` anywhere in your application's templates:
```html
```
---
## 2. Why Use Global Registration?
### Global Availability
By registering a component via `app.component()`, it becomes globally accessible. You do not need to repeatedly write `import MyComponent from './MyComponent.vue'` and declare it in the `components` option of every parent component. This significantly simplifies template usage.
### Code Reusability
Global registration is ideal for highly reusable UI elements. It ensures that common building blocks are instantly available across different views, layouts, and nested components, reducing boilerplate code.
### Centralized Management
Registering components globally allows you to centralize your core UI library setup in a single entry point (like `main.js` or a dedicated plugin file). This makes it easier to manage, update, and audit your shared components.
---
## 3. Common Use Cases
### Common UI Components (Design Systems)
Base UI components that appear frequently across your entire application are prime candidates for global registration. Examples include:
* Buttons (`BaseButton`)
* Input fields (`BaseInput`)
* Icons (`BaseIcon`)
* Spinners and Loaders (`BaseSpinner`)
### Functional and Global Overlays
Components that can be triggered from various parts of the application layout:
* Modals and Dialogs
* Toast notifications
* Tooltips
### Third-Party Component Libraries
When integrating UI libraries like Element Plus, Ant Design Vue, or Vuetify, you will often use `app.component()` (usually handled automatically via their Vue plugins) to make their components globally available.
---
## 4. Practical Code Examples
### Example A: Registering a Component with Options API
```javascript
import { createApp } from 'vue';
const app = createApp({});
app.component('click-counter', {
data() {
return {
count: 0
};
},
template: `
`
});
app.mount('#app');
```
### Example B: Registering Single File Components (SFCs)
In modern Vue development, you typically write components in `.vue` files. You can import and register them globally in your `main.js` or `main.ts` file:
```javascript
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import BaseButton from './components/BaseButton.vue';
import BaseIcon from './components/BaseIcon.vue';
const app = createApp(App);
// Register Single File Components globally
app.component('BaseButton', BaseButton);
app.component('BaseIcon', BaseIcon);
app.mount('#app');
```
---
## 5. Important Considerations
### Component Naming Conventions
It is highly recommended to use descriptive, multi-word names for your components to avoid conflicts with existing or future HTML elements (e.g., use `TodoItem` or `todo-item` instead of just `item`).
* **PascalCase** (e.g., `MyComponent`) is recommended for JS registration because it works well with both PascalCase and kebab-case (``) in templates.
### Avoid Overusing Global Registration
While global registration is convenient, it has a few drawbacks:
* **Bundle Size (Tree Shaking)**: Globally registered components cannot be tree-shaken by build tools like Webpack or Vite. Even if you do not use a global component anywhere in your code, it will still be included in the final production bundle.
* **Dependency Obscurity**: It makes relationships between components less explicit. A developer looking at a template might find it harder to trace where a component is defined if it is registered globally.
### When to Choose Local Registration
For components that are only used in one or a few specific places (e.g., a `UserProfileCard` used only on the profile page), use **local registration** instead:
```html
```
YouTip