Vue Components

Reusable building blocks.

✍️ 3 exercises + code challenge in this lesson

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 template option holds the component's HTML.

Key Takeaways: Vue Components

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.

Vue Components Code Example

Vue.js
<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>
Free sign-in required · Google login available

Vue Components Practice Exercises

Check what you learned in “Vue Components”.

/ 3 solved
Fill in the blank

Register a global component.

app.("my-card", { template: "<div>Card</div>" });
✓ Correct! Not quite. Try again.
Multiple choice

Do two instances of the same component share their data()?

✓ Correct! Not quite. Try again.
Code challenge

Register a component "hello-box" with the template <p class="hello">Hello!</p> and use it twice.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

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