Single-file components
Single-File Components (SFCs) are files with a .vue
extension that contain the complete definition of a single component and can be imported into your Vue.js app. SFCs make it simple to create and use components, and come with a variety of other benefits which we'll soon explore.
SFCs are similar to HTML files but have (at most) three root elements:
template
script
style
The component definition goes inside the script
tag and will be exactly like any other component definition except:
- It will export an ES module
- It will not need a
template
property (or arender
function; more on that later)
The component's template will be declared as HTML markup inside the template
tag. This should be a welcome relief from writing cumbersome template strings!
The style
tag is a feature unique to SFCs and can contain any CSS rules you need for the component. This mostly just helps with the organization of your CSS.
Here's an example of the declaration and usage of a single-file component.
MyComponent...