Vue First SFC Web Page

🌟 Vue First SFC Web Page (Single File Component) Web Page

In Vue.js, a Vue First SFC Web Page is the standard and recommended way to build Vue applications.

An SFC keeps HTML, JavaScript, and CSS together in one .vue file.


 What is a Vue SFC?

A Single File Component is a .vue file that contains:

<template> <!-- HTML -->
<script> <!-- JavaScript -->
<style> <!-- CSS -->

✔ Clean
✔ Organized
✔ Scalable
✔ Industry standard


🧱 Step 1: Create a Vue Project (Using Vite)

Run this in terminal:

npm create vue@latest

Then:

cd project-name
npm install
npm run dev

Open browser at:
👉 http://localhost:5173


🧩 Step 2: Understand Default Structure

src/
├─ components/
│ └─ HelloWorld.vue
├─ App.vue
└─ main.js

🧠 Step 3: main.js (Entry Point)

import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount(‘#app’)

✔ Loads App.vue
✔ Mounts app to index.html


🧩 Step 4: Your First SFC – App.vue

This is your first Vue SFC web page 👇


 


 What’s Happening Here?

🧩 <template>

<h1>{{ title }}</h1>

✔ Displays dynamic data
✔ Uses Vue template syntax


🧠 <script>

data() {
return { title, message }
}

✔ Holds logic & state
methods handle events


🎨 <style>

.container { text-align: center; }

✔ Styles your component


🔒 Scoped Style (Recommended)

<style scoped>
h1 {
color: green;
}
</style>

✔ CSS applies only to this component


🧩 Step 5: Create Another SFC Component

📄 src/components/Home.vue

<template>
<h2>Home Component</h2>
</template>
<script>
export default {
name: “Home”
}
</script>

Use it in App.vue:

<script>
import Home from './components/Home.vue'
export default {
components: { Home }
}
</script>

<Home />

 Why SFC is Best for Real Projects

Feature Benefit
Single file Easy maintenance
Scoped CSS No style conflicts
Reusable Component-based
Tooling IDE & build support

❌ Common Beginner Mistakes

❌ Forgetting export default
❌ Writing logic in <template>
❌ Not using scoped styles
❌ Large components (split them)


✅ Best Practices

✔ One component = one responsibility
✔ Keep SFC under control
✔ Use components folder
✔ Use scoped styles
✔ Move logic to composables later


🏁 Conclusion

Your first Vue SFC web page is now ready 🎉
Single File Components are the backbone of professional Vue applications.

You may also like...