Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧭 What You’ll Learn
In this chapter, you'll master how to:
🧱 What Are Components in
Vue?
Vue components are self-contained, reusable UI elements
that help you break down a complex application into modular parts.
Each component can contain:
🔁 Benefits of Reusable
Components
📦 Component Structure
Example
vue
<!--
components/MyButton.vue -->
<template>
<button class="my-btn"
@click="handleClick">
<slot />
</button>
</template>
<script>
export
default {
name: 'MyButton',
emits: ['click'],
methods: {
handleClick() {
this.$emit('click')
}
}
}
</script>
<style
scoped>
.my-btn
{
padding: 10px 20px;
background-color: #42b983;
color: white;
border: none;
border-radius: 4px;
}
</style>
📁 Recommended Component
Folder Structure
bash
src/
├── components/
│ ├──
BaseButton.vue
│ ├──
Card.vue
│ ├──
Navbar.vue
│ └── Footer.vue
├── views/
│ ├──
Home.vue
│ └── About.vue
🧩 Suggested Naming
Convention
Type |
Prefix |
Example |
Base elements |
Base |
BaseButton.vue |
Layouts |
Layout |
LayoutHeader.vue |
Views |
None |
Home.vue |
Modules |
Functional |
PostCard.vue |
🛠️ Registering
Components
A. Local Registration (Preferred)
vue
<script>
import
BaseButton from './components/BaseButton.vue'
export
default {
components: {
BaseButton
}
}
</script>
B. Global Registration
main.js:
js
import
BaseButton from './components/BaseButton.vue'
app.component('BaseButton',
BaseButton)
🎯 Using Props
Props allow components to accept dynamic input from parent
components.
Parent.vue:
vue
<UserCard
:name="'Alice'" :age="30" />
UserCard.vue:
vue
<script>
export
default {
props: {
name: String,
age: Number
}
}
</script>
🔁 Emitting Events
Child components can communicate back using $emit.
Child.vue:
vue
<template>
<button
@click="$emit('increment')">Add</button>
</template>
Parent.vue:
vue
<Counter
@increment="count++" />
🎨 Using Slots
Slots allow you to inject custom content into a
component’s template.
BaseCard.vue:
vue
<template>
<div class="card">
<slot />
</div>
</template>
Usage:
vue
<BaseCard>
<h3>Card Title</h3>
<p>Some custom content inside the
card.</p>
</BaseCard>
📚 Building Common
Reusable Components
🟢 BaseButton.vue
vue
<template>
<button :class="btnClass"
@click="$emit('click')">
<slot />
</button>
</template>
<script>
export
default {
props: {
variant: {
type: String,
default: 'primary'
}
},
computed: {
btnClass() {
return `btn-${this.variant}`
}
}
}
</script>
<style
scoped>
.btn-primary
{ background: #42b983; color: white; }
.btn-secondary
{ background: #ccc; color: #333; }
</style>
🟡 Card.vue
vue
<template>
<div class="card">
<slot name="header" />
<slot />
<slot name="footer" />
</div>
</template>
<style
scoped>
.card
{
border: 1px solid #eee;
border-radius: 6px;
padding: 20px;
margin: 10px 0;
}
</style>
🔵 Navbar.vue
vue
<template>
<nav>
<router-link
to="/">Home</router-link>
<router-link
to="/about">About</router-link>
</nav>
</template>
<style
scoped>
nav
{
background: #f0f0f0;
padding: 10px;
}
nav
a {
margin-right: 10px;
text-decoration: none;
}
</style>
🧬 Scoped vs Global Styles
Type |
Usage |
Scope |
Scoped |
<style scoped>
in .vue files |
Affects only component |
Global |
App.vue,
main.css, or public/index.html |
Affects
entire app |
🔄 Lifecycles for Reusable
Components
Use lifecycle hooks like:
🧪 Example: Watching Prop
vue
<script>
export
default {
props: ['count'],
watch: {
count(newVal) {
console.log('Count updated:', newVal)
}
}
}
</script>
📊 Feature Comparison
Table: Props, Events, Slots
Feature |
Purpose |
Use Case Example |
Props |
Pass data into
component |
:title="Hello" |
Events |
Emit action
to parent |
@click="doSomething" |
Slots |
Insert content into
component |
<slot
name="header">...</slot> |
✅ Best Practices
💡 Real-World Use Cases
Component |
Usage Scenario |
<BaseButton> |
Action buttons with
different styles |
<Card> |
Reusable
content wrappers |
<Modal> |
Confirmation dialogs,
form popups |
<Navbar> |
App
navigation layout |
<Toast> |
Temporary feedback
messages |
✅ Component Checklist
Task |
Done |
Component created
using .vue |
✅ |
Props defined and validated |
✅ |
Events emitted as
needed |
✅ |
Slots used for flexibility |
✅ |
Scoped styles applied |
✅ |
File organized in /components |
✅ |
Answer:
A Single Page Application is a web app that loads a single HTML file and
dynamically updates the content without refreshing the page. This allows for a
smoother user experience, similar to desktop or mobile apps.
Answer:
Vue.js is lightweight, beginner-friendly, and has a powerful ecosystem. It
supports component-based architecture and works seamlessly with Vue Router for
SPA navigation. It’s a great choice for fast, reactive, and scalable SPAs.
Answer:
Answer:
Vue Router handles navigation in the browser without reloading pages. It maps
URL paths to Vue components and updates the view dynamically using the
browser’s History API.
Answer:
Technically yes, but it’s not recommended. Vue Router provides essential SPA
features like URL mapping, history handling, navigation guards, and lazy
loading.
Answer:
Both are state management libraries. Vuex is the older, more complex
option, while Pinia is the official replacement for Vue 3—lighter,
easier to use, and modular.
Answer:
Yes, but it's only suitable for small-scale SPAs. For larger projects, using
the Vue CLI or Vite offers better file organization, hot reloading, and tooling
support.
Answer:
SPAs typically have poor SEO out of the box because content is rendered via
JavaScript. Use pre-rendering (e.g., with Prerender.io) or switch to Server-Side Rendering (SSR) with
Nuxt.js for better SEO.
Answer:
You can deploy your Vue SPA on:
Answer:
Yes. Vue.js is modular and scalable. With features like lazy loading, component
splitting, Pinia/Vuex for state management, and Vue Router, it can power large,
production-ready SPAs effectively.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)