Discover how smart component registration saves you from endless imports and messy code!
Component registration (global vs local) in Vue - When to Use Which
Imagine building a Vue app with many components and manually importing and registering each one everywhere you want to use it.
You have to remember to import and register each component in every parent component that needs it.
This manual approach is tiring and error-prone.
You might forget to register a component, causing errors or blank spaces in your app.
It also clutters your code with repetitive imports and registrations.
Vue lets you register components globally or locally.
Global registration means you register once and use the component anywhere without extra imports.
Local registration keeps components registered only where needed, keeping code clean and optimized.
import MyButton from './MyButton.vue'; export default { components: { MyButton } }
import MyButton from './MyButton.vue'; app.component('MyButton', MyButton) // global registration // or export default { components: { MyButton } } // local registration
This makes your app easier to build, maintain, and scale by managing components smartly.
In a large app, you can globally register common UI elements like buttons and icons once, and locally register unique components only where needed.
Manual imports and registrations are repetitive and error-prone.
Global registration allows reuse without repeated imports.
Local registration keeps code clean and optimized.
Practice
Solution
Step 1: Understand global registration
Global registration means the component can be used in any template without importing it again.Step 2: Understand local registration
Local registration means the component is only available inside the component where it is registered.Final Answer:
Global registration makes a component available everywhere, local limits it to one component. -> Option CQuick Check:
Global = everywhere, Local = one component [OK]
- Confusing global with local availability
- Thinking local registration is automatic
- Assuming global requires manual import every time
Solution
Step 1: Identify local registration syntax
Local registration uses the components option inside the component definition object.Step 2: Match correct syntax
The syntax is components: { MyComponent } to register locally.Final Answer:
components: { MyComponent } -> Option DQuick Check:
Local registration uses components: { } [OK]
- Using Vue.component() for local registration
- Confusing import statement with registration
- Trying to use globalComponents option which doesn't exist
<ParentComponent/>?
const ChildComponent = {
template: 'Child here
'
}
const ParentComponent = {
template: '',
components: { ChildComponent }
}
app.component('parent-component', ParentComponent)
app.mount('#app')Solution
Step 1: Check local registration in ParentComponent
ChildComponent is registered locally inside ParentComponent using components: { ChildComponent }.Step 2: Understand template rendering
The tag inside ParentComponent's template will render ChildComponent's templateChild here
only if the tag name matches the registered component name in kebab-case.Step 3: Identify the issue
The component is registered as ChildComponent but used as , which Vue treats as a different tag. Since 'child-component' is not globally registered, Vue will throw an unknown custom element error.Final Answer:
Error: Unknown custom element <child-component> -> Option BQuick Check:
Component names are case-sensitive and must match usage [OK]
- Assuming PascalCase component names automatically match kebab-case tags
- Forgetting to register components globally when using kebab-case tags
- Expecting local registration to work with mismatched tag names
const app = Vue.createApp({})
app.component('MyButton', {
template: 'Click me'
})
const AnotherComponent = {
template: ''
}
app.component('AnotherComponent', AnotherComponent)
app.mount('#app')Solution
Step 1: Check component registration
MyButton is registered globally using app.component('MyButton', ...), so it is available everywhere.Step 2: Check template tag casing
Vue requires component tags in templates to be kebab-case or PascalCase but HTML is case-insensitive, so should be .Final Answer:
The <MyButton /> tag inside AnotherComponent template should be lowercase <my-button />. -> Option AQuick Check:
Component tags must be kebab-case in templates [OK]
- Using PascalCase tags directly in templates
- Thinking global registration needs import in every component
- Assuming Vue.createApp() can't register components
Solution
Step 1: Consider usage frequency
The button is used many places, so global registration avoids repeated imports and registrations.Step 2: Consider component scope
The modal is used only inside one page component, so local registration keeps scope limited and reduces global clutter.Final Answer:
Register the button component globally and the modal component locally inside the page component. -> Option AQuick Check:
Global for many uses, local for few uses [OK]
- Registering rarely used components globally unnecessarily
- Registering widely used components locally repeatedly
- Ignoring app size and performance considerations
Ready to prove it?
Pass the test and this pattern moves unknown
