Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Component Basics

Vue.jsLesson 1020 min

What you'll walk away with

  • Understand the structure of a Vue SFC
  • Import and use a child component

Explain it simply

Component Basics, in plain language

A Vue component can have <script setup>, <template>, and an optional <style scoped>. Split components into small pieces, each with one feature or responsibility. Once a component is imported in <script setup>, you can use it directly in the template.

Problem → Why → Solution

The problem — why this concept matters

Cramming every feature into a single component

A common mistake

Using a component's tag in an SFC without importing it

Core concept — understand how it works

Split the UI into separate, reusable pieces using Single-File Components.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Understand the structure of a Vue SFC

Core syntax or pattern

vue
<!-- CounterButton.vue -->
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <button @click="count++">Clicked {{ count }} times</button>
</template>

<style scoped>
button { padding: .75rem 1rem; }
</style>

Split the UI into separate, reusable pieces using Single-File Components.

Read the code step by step

  1. props or event<!-- CounterButton.vue -->

    A Vue component can have <script setup>, <template>, and an optional <style scoped>. Split components into small pieces, each with one feature or responsibility. Once a component is imported in <script setup>, you can use it directly in the template.

  2. component and template<template>

    Split the UI into separate, reusable pieces using Single-File Components.

  3. rendered Vue UI</style>

    Apply Component Basics in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Understand the structure of a Vue SFC

Failure

Cramming every feature into a single component

Let's break it down simply

A Vue component can have <script setup>, <template>, and an optional <style scoped>. Split components into small pieces, each with one feature or responsibility. Once a component is imported in <script setup>, you can use it directly in the template.

vue
<!-- CounterButton.vue -->
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <button @click="count++">Clicked {{ count }} times</button>
</template>

<style scoped>
button { padding: .75rem 1rem; }
</style>
You should see
Clicked 0 times

Try it yourself

Create an AvatarCard.vue component and reuse it three times inside App.vue.

Components Basics — Vue.js

Real-world usage

How to apply it in a real project

Apply Component Basics in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Create an AvatarCard.vue component and reuse it three times inside App.vue.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<!-- CounterButton.vue -->
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <button @click="count++">Clicked {{ count }} times</button>
</template>

<style scoped>
button { padding: .75rem 1rem; }
</style>

Expected: Understand the structure of a Vue SFC

Show hint

Apply Component Basics in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Component Basics?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Split the UI into separate, reusable pieces using Single-File Components.
  • Cramming every feature into a single component
  • Apply Component Basics in a small example and test normal, edge, and failure cases.

Components Basics — Vue.js

Easy traps

  • Cramming every feature into a single component
  • Using a component's tag in an SFC without importing it

Exercise

Create an AvatarCard.vue component and reuse it three times inside App.vue.

You'll know it worked when: Clicked 0 times

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.