SFC Syntax Specification
This page documents the complete syntax specification for Vue Single-File Components (SFCs).Basic Structure
A Vue SFC is a custom file format using.vue extension that encapsulates a component’s template, logic, and styling:
Language Blocks
<template>
Defines the component’s template markup.
Attributes:
lang- Template language (e.g.,html,pug,jade)src- Import template from external file
- Each SFC can contain at most one
<template>block - Content is extracted and passed to
@vue/compiler-domand compiled into render functions - Supports all standard Vue template syntax
<script>
Defines the component’s logic using standard JavaScript or TypeScript.
Attributes:
lang- Script language (e.g.,js,ts,jsx,tsx)src- Import script from external filesetup- Declares as<script setup>(see SFC Script Setup)
- Each SFC can contain at most one
<script>block (excluding<script setup>) - Can coexist with
<script setup>in the same component - Must export component definition as default export (unless using
<script setup>)
<script setup>
A compile-time syntactic sugar for using Composition API inside SFCs.
Attributes:
lang- Script language (e.g.,ts,tsx)generic- Declare generic type parameters
- Each SFC can contain at most one
<script setup>block - Cannot use
srcattribute (syntax would be ambiguous) - Top-level bindings are automatically exposed to template
- Macros like
defineProps(),defineEmits()available without imports
<style>
Defines component styles.
Attributes:
lang- Preprocessor language (e.g.,css,scss,sass,less,stylus,postcss)scoped- Apply styles to current component onlymodule- Enable CSS Modules (can be boolean or string for custom inject name)src- Import styles from external file
- Multiple
<style>tags are supported - Can mix scoped and non-scoped styles
- Scoped styles apply via PostCSS transformation
Custom Blocks
Project-specific custom blocks can be included in SFCs:vite-plugin-vue).
Source Imports
All language blocks support thesrc attribute to import content from external files:
- Uses webpack module request syntax
- Supports relative paths (
./ ../) and npm packages - Cannot be used on
<script setup>(syntax ambiguity) - If both
<script>and<script setup>present, neither can usesrc
Preprocessors
Preprocessors are enabled via thelang attribute:
TypeScript
Pug
Sass/SCSS
Comments
Inside each block, use comment syntax of that language:Parsing Behavior
parseMode: ‘sfc’
The compiler parses SFCs withparseMode: 'sfc':
- Top-level tags are treated as language blocks
- Only one
<template>, one<script>, and one<script setup>allowed - Multiple
<style>and custom blocks allowed - Empty blocks are ignored (except
<template>)
Error Handling
The parser will error when:- No
<template>or<script>present - Duplicate
<template>,<script>, or<script setup>blocks <script setup>usessrcattribute- Both
<script>and<script setup>present, and<script>usessrc
Functional Components
Vue 2’s<template functional> is deprecated:
<script setup> instead, as functional components no longer have significant performance benefits in Vue 3.
API Reference
parse()
Parses a raw.vue file into a descriptor: