Single-File Components
A single-file component is a Vue component in one file. The file holds the code and the markup together.
The script block
This block holds one plain const.
<script setup>
const message = 'Hello, Vue!'
</script>The markup block
This line shows the value from the script block.
<h1>{{ message }}</h1>The file shape
This file holds the code and the markup together.
<script setup>
const message = 'Hello, Vue!'
</script>
<template>
<h1>{{ message }}</h1>
</template>Example
<script setup>
const message = 'Hello, Vue!'
</script>
<template>
<div>
<h1>{{ message }}</h1>
<p>A single file holds this page.</p>
</div>
</template>Key points
- A single-file component is a Vue component in one file.
- The file name ends with .vue.
- The script block starts with <script setup> and holds JavaScript code.
- A plain const holds a value that stays the same.
In this example, the script block holds a plain const. The const is named message. Its value is Hello, Vue!. The markup block shows that value in a heading. A short line follows the heading.
Note
The markup block starts with <template> and holds the page markup.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does a single-file component hold in one file?