Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
Vue components are reusable Vue instances with a name. There are two types of component registration: local registration and global registration.
Local Registration
When you need a component to be only accessible in certain components, you can use local registration:
<template> <my-component></my-component> </template> <script> import MyComponent from './MyComponent.vue' export default { components: { MyComponent } } </script>
In this case, MyComponent
is only accessible within the component it was registered in.
Global Registration
If a component needs to be used across many components, it can be more convenient to register the component globally. All components registered globally can be used in the template of any root Vue instance (new Vue) created afterwards:
import { createApp } from 'vue' import MyComponent from './MyComponent.vue' import App from './App.vue' const app = createApp(App) app.component('my-component', MyComponent) app.mount('#app')
In this case, MyComponent
is globally registered and can be used anywhere in your Vue app.
Async Components
When registering a component globally, you can also define it as an asynchronous component. This means Vue will only fetch the component code when the component actually needs to be rendered, which can help improve performance:
import { createApp, defineAsyncComponent } from 'vue' import App from './App.vue' const app = createApp(App) const MyComponent = defineAsyncComponent(() => import('./MyComponent.vue')) app.component('my-component', MyComponent) app.mount('#app')
Here, MyComponent
is defined as an asynchronous component with defineAsyncComponent
. The function passed to defineAsyncComponent
will be called the first time the component needs to be rendered.
This is a brief overview of how to register components in Vue 3. There are more advanced topics like dynamic & async component, component names conventions and caveats, etc. You can learn more about these topics in the Vue.js documentation.
Vue 3.0 Global Component Registration:
Register a component globally using app.component
. This makes it available throughout the entire application.
const MyComponent = { // ...component options }; app.component('my-component', MyComponent);
Vue 3.0 Local Component Registration:
Register a component locally within another component's components
option.
const ParentComponent = { components: { 'my-component': MyComponent, }, // ...component options };
Vue 3.0 Automatic Component Registration: Components defined with PascalCase in the app's components option are automatically registered.
const app = createApp({ components: { MyComponent, }, });
Component Naming Conventions in Vue 3.0: Follow the convention of using PascalCase for component names in templates and kebab-case for registration.
<!-- In template --> <my-component></my-component> <!-- In registration --> app.component('my-component', MyComponent);
Vue 3.0 Registration with app.component
Method:
Use app.component
for manual component registration.
const MyComponent = { // ...component options }; app.component('my-component', MyComponent);
Vue 3.0 Registration with app.component
Directive:
Use app.component
directly in the template for inline registration.
<template> <my-component></my-component> </template> <script> import MyComponent from './MyComponent.vue'; export default { components: { 'my-component': MyComponent, }, // ...component options }; </script>
Vue 3.0 Async Component Registration:
Register components asynchronously using defineAsyncComponent
.
const AsyncComponent = defineAsyncComponent(() => import('./AsyncComponent.vue')); app.component('async-component', AsyncComponent);
Vue 3.0 Dynamic Component Registration: Dynamically register components based on conditions.
const dynamicComponent = condition ? DynamicComponentA : DynamicComponentB; app.component('dynamic-component', dynamicComponent);
Vue 3.0 Registration of Functional Components: Register functional components for stateless, logic-only components.
const FunctionalComponent = { functional: true, // ...render function }; app.component('functional-component', FunctionalComponent);
Vue 3.0 Conditional Component Registration: Conditionally register components based on app state or other conditions.
if (condition) { app.component('conditional-component', ConditionalComponent); }
Vue 3.0 Registering Components in Different Files: Organize components in separate files and import them for registration.
// In MyComponent.vue file export default { // ...component options } // In main file import MyComponent from './MyComponent.vue'; app.component('my-component', MyComponent);
Vue 3.0 Registration of Third-Party Components:
Register third-party components using app.component
or app.component
directive.
import ThirdPartyComponent from 'third-party-library'; app.component('third-party-component', ThirdPartyComponent);