Vue 3 Creating Single File Components (SFC)
Vue's Single File Component (i.e., the *.vue file, Single-File Component, abbreviated SFC) is a special file format that lets us encapsulate a Vue component's template, logic, and styles in a single file.
In Vue 3, you can use .vue files to create Single File Components (SFCs); this file contains the component's template, JavaScript code, and CSS styles.
Now we will delete all content in the example project created via the npm init vue@latest command (refer to Vue 3 Installation) so that you can create your own simple web page in Vue.
Before we start writing code, delete
,
At the same time we can first empty src the folders in the directory assets and components; we can add the files inside later ourselves. Modify the main.js file code to the following: import { createApp } from 'vue' import App from './App.vue' createApp ( App ) . mount ( '#app' ) Now we have created a simple project; write the following code in the App.vue file: {{ message }} In the example above we defined a simple component containing a title. The component's template contains a The JavaScript part contains a data function that returns an object containing the message property, which we bind to the template. Finally, the CSS styles define the title color as blue. In the JavaScript part of the component, we use the new export default syntax, which lets us export the component definition as a default object. In Vue 3, we can use this syntax to define components, instead of using the Vue.component function as in Vue 2. Access the http://localhost:5173/, the execution result of the above code is: After we have defined a component, we can use it in other components. To use a component, we first need to create it; for example, the following example creates it in ./src/components/ directory, creating HelloRunoob.vue component file, with the code as follows: {{ message }} Then we import and define the component in the ./src/main.js file: import { createApp } from 'vue' import App from './App.vue' import HelloRunoob from './components/HelloRunoob.vue' const app = createApp ( App ) app. component ( 'hello-runoob' , HelloRunoob ) // custom tag app. mount ( '#app' ) In the parent component's template, we can introduce child components using custom tags, as in the following App.vue file code: Access the http://localhost:5173/, the execution result of the above code is:main.js file code:
App.vue file code:
element, using Vue 3's template syntax to bind the message property to this element's text content.

Using Components
./src/components/HelloRunoob.vue file code:
./src/main.js file code:
App.vue file code
