Register a component with app.component("name", { ... }) and use it as a custom tag.
- Each component has its own
data(), so every instance has independent state. - The
templateoption holds the component's HTML.
Reusable building blocks.
✍️ 3 exercises + code challenge in this lessonRegister a component with app.component("name", { ... }) and use it as a custom tag.
data(), so every instance has independent state.template option holds the component's HTML.Mastering Vue Components helps you write clean and reliable Vue.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
<script src="https://unpkg.com/vue@3.5.12/dist/vue.global.prod.js"></script>
<div id="app">
<like-button></like-button>
<like-button></like-button>
</div>
<script>
const app = Vue.createApp({});
app.component("like-button", {
data() { return { likes: 0 }; },
template: `<button @click="likes++">👍 {{ likes }}</button>`
});
app.mount("#app");
</script>
Check what you learned in “Vue Components”.
Register a global component.
app.("my-card", { template: "<div>Card</div>" });
Do two instances of the same component share their data()?
Register a component "hello-box" with the template <p class="hello">Hello!</p> and use it twice.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services