π Vue Router Basics: Your Appβs GPS System
Imagine youβre in a giant shopping mall. Without a map or signs, youβd be lost! Vue Router is like the mallβs navigation systemβit tells your Vue app which βroomβ (page) to show based on where the visitor wants to go.
π The Big Picture
Think of your Vue app as a house with many rooms:
- The living room is your Home page
- The kitchen is your About page
- The bedroom is your Profile page
Vue Router is the magic doorman who:
- Knows which room you want to visit
- Opens the right door
- Shows you exactly whatβs inside
Without Vue Router, your house would have only ONE room. Boring! π΄
π¦ Router Installation
What Is It?
Installing Vue Router is like hiring that magic doorman for your house. You canβt navigate between rooms without him!
How To Do It
Step 1: Install the package
npm install vue-router@4
Thatβs it! You just hired your doorman. π
Step 2: Create the router file
Create a new file called router/index.js:
import { createRouter, createWebHistory }
from 'vue-router'
const router = createRouter({
history: createWebHistory(),
routes: []
})
export default router
Step 3: Tell Vue to use it
In your main.js:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App)
.use(router)
.mount('#app')
π― Real-Life Example
Itβs like downloading a map app on your phone. First you install it, then you open it, then you can navigate anywhere!
πΊοΈ Creating Routes
What Are Routes?
Routes are directions that tell Vue Router: βWhen someone visits THIS address, show THAT page.β
The Simple Version
const routes = [
{
path: '/',
component: Home
},
{
path: '/about',
component: About
}
]
π§© Breaking It Down
| Part | What It Means |
|---|---|
path |
The URL address (like /about) |
component |
The page to show |
π― Real-Life Example
Think of a restaurant menu:
- Path = βBurgerβ (what you order)
- Component = The actual burger that arrives
// Full example with imports
import Home from './views/Home.vue'
import About from './views/About.vue'
import Contact from './views/Contact.vue'
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/contact', component: Contact }
]
Every route is a promise: βVisit this URL, see this page!β
βοΈ Router Configuration
What Is Configuration?
Configuration is setting up the rules for how your doorman behaves. Does he remember where youβve been? Does he use clean URLs?
The Main Options
const router = createRouter({
// How URLs look and work
history: createWebHistory(),
// All your routes
routes: routes
})
π History Modes
1. Web History (Clean URLs) β¨ Recommended!
history: createWebHistory()
// URLs look like: /about, /contact
2. Hash History (Has # in URL)
history: createWebHashHistory()
// URLs look like: /#/about, /#/contact
π― Which Should You Use?
| Mode | URL Example | Best For |
|---|---|---|
| Web History | /about |
Most apps |
| Hash History | /#/about |
Simple hosting |
Full Configuration Example
import { createRouter, createWebHistory }
from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
})
export default router
πΊ RouterView Component
What Is RouterView?
<RouterView> is like a TV screen in your house. Whatever βchannelβ (route) youβre on, the TV displays that content.
How It Works
<template>
<div id="app">
<header>My App</header>
<!-- π This is the magic TV screen! -->
<RouterView />
<footer>Β© 2024</footer>
</div>
</template>
π― Visual Explanation
βββββββββββββββββββββββββββ
β HEADER β β Always visible
βββββββββββββββββββββββββββ€
β β
β <RouterView /> β β Changes based on URL!
β β
βββββββββββββββββββββββββββ€
β FOOTER β β Always visible
βββββββββββββββββββββββββββ
When you visit /about:
- Header stays the same
- RouterView shows the About page
- Footer stays the same
When you visit /contact:
- Header stays the same
- RouterView shows the Contact page
- Footer stays the same
π¬ Think Of It Like This
RouterView is the movie screen at a cinema. The seats, walls, and popcorn stand stay the sameβbut the movie (content) changes!
π RouterLink Component
What Is RouterLink?
<RouterLink> is a magic button that teleports visitors to different pages WITHOUT refreshing the whole app. Itβs much faster than regular links!
Basic Usage
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/about">About</RouterLink>
<RouterLink to="/contact">Contact</RouterLink>
Why Not Use Regular Links?
Regular <a> tag |
<RouterLink> |
|---|---|
| Refreshes whole page π’ | Instant switch β‘ |
| Loses app state | Keeps everything |
| Slow | Super fast |
π― Real-Life Example
Regular link = Taking a taxi to a new store (slow, starts over)
RouterLink = Walking through a door in a mall (instant, youβre still in the mall!)
Full Navigation Example
<template>
<nav>
<RouterLink to="/">π Home</RouterLink>
<RouterLink to="/about">βΉοΈ About</RouterLink>
<RouterLink to="/contact">π§ Contact</RouterLink>
</nav>
</template>
Dynamic Routes
You can also use variables:
<RouterLink :to="`/user/${userId}`">
View Profile
</RouterLink>
π¨ Active Link Styling
What Is Active Link Styling?
When youβre on a page, the link to that page should look differentβlike a glowing button saying βYou are HERE!β
How Vue Router Helps
Vue Router automatically adds special classes to active links:
| Class | When Itβs Added |
|---|---|
router-link-active |
Current route matches |
router-link-exact-active |
Exact match only |
Basic CSS Styling
/* Style for active links */
.router-link-active {
color: blue;
font-weight: bold;
}
/* Style for exact matches */
.router-link-exact-active {
color: green;
border-bottom: 2px solid green;
}
Custom Class Names
Want your own class names? Easy!
<RouterLink
to="/about"
active-class="my-active"
exact-active-class="my-exact-active"
>
About
</RouterLink>
.my-active {
background: yellow;
}
.my-exact-active {
background: gold;
transform: scale(1.1);
}
π― Visual Example
Navigation Bar:
ββββββββββββββββββββββββββββββββββββ
β Home [About] Contact β
β β β
β This one glows because β
β we're on the About page! β
ββββββββββββββββββββββββββββββββββββ
Complete Styled Navigation
<template>
<nav class="navbar">
<RouterLink to="/" class="nav-link">
Home
</RouterLink>
<RouterLink to="/about" class="nav-link">
About
</RouterLink>
</nav>
</template>
<style>
.nav-link {
padding: 10px 20px;
color: gray;
}
.nav-link.router-link-exact-active {
color: white;
background: #42b983;
border-radius: 5px;
}
</style>
πΊοΈ Putting It All Together
Hereβs a complete mini-app using everything you learned:
graph TD A["User Types URL"] --> B{Vue Router} B --> C["Finds Matching Route"] C --> D["Loads Component"] D --> E["Shows in RouterView"] E --> F["User Sees Page!"]
Complete Example
router/index.js
import { createRouter, createWebHistory }
from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
})
export default router
App.vue
<template>
<div id="app">
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/about">About</RouterLink>
</nav>
<RouterView />
</div>
</template>
<style>
.router-link-exact-active {
font-weight: bold;
color: #42b983;
}
</style>
π Quick Summary
| Concept | What It Does | One-Line Example |
|---|---|---|
| Installation | Adds router to your app | npm install vue-router@4 |
| Routes | Maps URLs to pages | { path: '/', component: Home } |
| Configuration | Sets up router rules | createRouter({ history, routes }) |
| RouterView | Displays current page | <RouterView /> |
| RouterLink | Navigation without refresh | <RouterLink to="/"> |
| Active Styling | Highlights current link | .router-link-active { } |
π You Did It!
You now understand Vue Router basics! Think of it like this:
π Your app is a house πͺ Routes are doors to different rooms π§ Router is the GPS that knows where you want to go πΊ RouterView is the TV showing current content π RouterLink is the teleporter between rooms π¨ Active styling is the βYou Are Hereβ marker
Now go build something amazing! π
