Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Components  ›  Lesson

Registering Components

Component Registration 11 minImporting a component inside script setup registers it locally, while app.component registers it globally for every component.

Registering Components

You have written BookCard.vue. Now BookList.vue wants to show several of them. Writing <BookCard /> in its template does nothing yet: Vue has to be told what the tag BookCard means. That is what registration does.

The rule

Import it to use it here; app.component to use it everywhere.

Local registration is the everyday way. In <script setup>, importing a component is all it takes:

<!-- BookList.vue -->
<script setup>
import BookCard from './BookCard.vue'
</script>

<template>
  <section>
    <BookCard />
    <BookCard />
  </section>
</template>

The import registers BookCard for this one file. Other components cannot use it unless they import it too.

Global registration makes a component available in every template of the app. You do it once on the app instance, before mounting:

// main.js
import { createApp } from 'vue'
import App from './App.vue'
import BaseButton from './components/BaseButton.vue'

createApp(App)
  .component('BaseButton', BaseButton)
  .mount('#app')

Global looks convenient, but it has costs. A global component is included in the final bundle even if no page uses it, because the build tool cannot tell. And a reader of BookList.vue cannot see where BaseButton comes from. That is why local registration is the default choice.

Names: use PascalCase for component names, like BookCard. In a .vue template you may write <BookCard /> or <book-card />; both work. If you ever write templates directly inside an HTML page (an "in-DOM" template), the browser lower-cases tag names, so there you must use <book-card></book-card> with a closing tag.

The shortcut

Ask one question: "How many files need this component?" One or a few: import it where it is used. Truly every screen, and small and stable: global is allowed, though importing still works.

Registering Shelf's componentsStep 1 of 4
  1. BookCard

    Only BookList shows cards. Import it in BookList.vue. Local.

The trap is registering globally out of habit. The app still works, but the bundle carries unused code and dependencies become invisible.

Drills

Answer fast — trust the rule.

quick drill

In <script setup>, what registers BookCard for use in that file's template?

quick drill

Where does app.component('BaseButton', BaseButton) make BaseButton available?

quick drill

Which is a genuine downside of global registration?

quick drill

You write a template directly inside index.html (an in-DOM template). How must you write BookCard's tag?

Remember

  • Import in <script setup> = local registration; app.component(name, comp) = global.
  • Shortcut: "how many files need it?" Few means import.
  • Trap: global by habit hides dependencies and bloats the bundle.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainLocal or global?

Shelf has a BookCard used only by BookList, and a BaseButton used on nearly every screen. Explain how you would register each one and why.