🎓 A1Learner
🔍
0 of 50 lessons completed

Introduction to Vue Components

A component is a small reusable piece of a page. It bundles its own markup and logic into one named unit.

A small component

defineComponent builds a component from a template string.

const Greeting = defineComponent({
  template: '<p>Hello from a component.</p>'
})

Using the component

The parent places the child in its template.

<Greeting />

Using it twice

Each use renders its own copy.

<Greeting />
<Greeting />

Example

<script setup>
import { defineComponent } from 'vue'
const Greeting = defineComponent({
  template: '<p>Hello from a component.</p>'
})
</script>
<template>
  <div>
    <Greeting />
    <Greeting />
  </div>
</template>

Key points

  • A component has a name, like Greeting.
  • A component can be used more than once in a page.
  • Each use of a component renders its own copy.
  • Components keep a large page split into small parts.
  • The component that uses another is called the parent.
  • The component being used is called the child.

In this example, Greeting is a small component with one paragraph. It is built with defineComponent and a template string. The parent template uses Greeting twice. Each use renders its own copy of the paragraph.

Note

A component name starts with a capital letter in the template.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What is a Vue component?