Vue Components
So far each example was one big template. Real apps are built from components: self-contained pieces of UI — a button, a product card, a navigation bar, a whole page — each with its own template, data and logic. You build small components and combine them like LEGO bricks.
App
├── NavBar
├── ProductList
│ ├── ProductCard
│ ├── ProductCard
│ └── ProductCard
└── CartSummaryRegistering a component
A component is just an options object, like the one you pass to createApp, but with a template. Register it globally with app.component(name, options), then use it as a custom tag.
<div id="app">
<h3>Three independent counters</h3>
<click-counter></click-counter>
<click-counter></click-counter>
<click-counter></click-counter>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({});
app.component('click-counter', {
data() {
return { count: 0 };
},
template: `<button @click="count++">Clicked {{ count }} times</button> `
});
app.mount('#app');
</script>Each <click-counter> is a separate instance with its own count. That is why data must be a function: it returns a fresh object for every instance.
Components with setup()
Components can use the Composition API just like the root app. This one also uses another component inside its template.
<style>
.card { border: 1px solid #ddd; border-radius: 8px; padding: 10px 14px; margin: 6px 0; max-width: 280px; }
.badge { background: #42b883; color: white; border-radius: 10px; padding: 0 8px; font-size: 12px; }
</style>
<div id="app">
<profile-card></profile-card>
</div>
<script type="module">
import { createApp, ref } from 'vue';
const app = createApp({});
app.component('online-badge', {
template: `<span class="badge">online</span>`
});
app.component('profile-card', {
setup() {
const name = ref('Linh Nguyen');
const followers = ref(120);
const follow = () => followers.value++;
return { name, followers, follow };
},
template: `
<div class="card">
<strong>{{ name }}</strong> <online-badge />
<p>{{ followers }} followers</p>
<button @click="follow">Follow</button>
</div>
`
});
app.mount('#app');
</script>Notice <online-badge /> is self-closing here. That's allowed because it lives in a string template, compiled by Vue. In the in-DOM template (inside <div id="app">) you must write <profile-card></profile-card>.
Local registration
Global components are available everywhere, even if unused — which makes bundles bigger and dependencies unclear. Local registration makes a component available only in the component that lists it in components.
<div id="app"></div>
<script type="module">
import { createApp } from 'vue';
const StarIcon = {
props: ['filled'],
template: `<span style="color:#f59e0b; font-size: 22px">{{ filled ? '★' : '☆' }}</span>`
};
const RatingRow = {
components: { StarIcon }, // local registration
data: () => ({ value: 2 }),
template: `
<div>
<span v-for="n in 5" :key="n" @click="value = n" style="cursor:pointer">
<StarIcon :filled="n <= value" />
</span>
({{ value }}/5)
</div>
`
};
createApp({
components: { RatingRow },
template: `<h3>Rate this lesson</h3><RatingRow /><RatingRow />`
}).mount('#app');
</script>In string templates and SFCs you can use PascalCase tags like <RatingRow />. Vue resolves both RatingRow and rating-row to the same component.
Global (app.component) |
Local (components: {…}) |
|
|---|---|---|
| Available in | Every component of the app | Only where it's listed |
| Tree-shaking | ❌ always bundled | ✅ unused ones can be dropped |
| Dependencies visible | ❌ hidden | ✅ explicit in each file |
| Good for | A handful of base components (BaseButton, AppIcon) |
Everything else |
Naming components
- Use multi-word names (
TodoItem, notItem) so they never clash with present or future HTML elements. - Register with PascalCase (
TodoItem); use<TodoItem>in SFC/string templates and<todo-item>in in-DOM templates.
In-DOM template rules (recap)
Because the browser parses <div id="app">…</div> before Vue does:
| Rule | ✅ Write | ❌ Not |
|---|---|---|
| kebab-case tags | <todo-item> |
<TodoItem> |
| explicit closing tags | <todo-item></todo-item> |
<todo-item /> |
| kebab-case props & events | :due-date="d" @item-done="f" |
:dueDate @itemDone |
components inside <ul>, <table>, <select> |
<li is="vue:todo-item"></li> |
<todo-item> directly |
Tip: In a real project every component lives in its own
.vuefile (Single-File Component) and none of these in-DOM caveats apply. The playgrounds usetemplatestrings so they can run without a build step.
Components inside a list
Components can be repeated with v-for like elements. Give each one a :key. Passing data into a component is done with props, the topic of the next lesson.
<div id="app">
<ul>
<li is="vue:fruit-row" v-for="f in fruits" :key="f" :name="f"></li>
</ul>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ fruits: ['🍎 Apple', '🍌 Banana', '🍇 Grape'] }) });
app.component('fruit-row', {
props: ['name'],
data: () => ({ liked: false }),
template: `<li>{{ name }} <button @click="liked = !liked">{{ liked ? '❤️' : '🤍' }}</button></li>`
});
app.mount('#app');
</script>is="vue:fruit-row" tells Vue to render the component in place of the <li>, which keeps the browser's <ul> rules happy.
Test yourself
Why must a component's data option be a function?
- Because Vue only accepts functions
- So each instance gets its own fresh data object
- To make it reactive faster
- So it can be async
How do you use a component named UserCard inside an in-DOM template?
<UserCard/><user-card></user-card><usercard/><UserCard></UserCard>
What is an advantage of local registration over global registration?
- Components can be used in every file without importing
- Unused components can be tree-shaken and dependencies are explicit
- Local components don't need a template
- Local components can't have props