Route Navigation
In this chapter you will learn to use Vue Router to implement front-end/back-end separated page navigation, including dynamic route parameters.
Why Use Routing?
On traditional multi-page websites, clicking a link requests a new HTML file from the server; the whole page refreshes, causing a white flash.
What Vue Router implements is front-end routing: when the URL changes, no request is sent to the server; instead, JS dynamically switches the displayed component.
The benefit: switching pages without refresh, with a smooth experience like a native app.
Vue Router Core Concepts
| Concept | Purpose | Corresponding Code |
|---|---|---|
| Route Table | Defines the mapping between URL paths and components | routes arrays |
| Router Outlet | The location where the matched component is displayed | |
| Navigation Link | Click to navigate to a specified route | |
| Dynamic Route | URL contains variables (e.g., article ID) | /post/:id |
Installation and Configuration
If you selected Router when creating the project, skip the installation step.
If not, run in the terminal:
$ npm install vue-router@4
Create the Router Configuration File
Example
// File path: src/router/index.js
import
{
createRouter
,
createWebHistory
}
from
'vue-router'
import
HomeView from
'../views/HomeView.vue'
// Route table: each record maps a path to a component
const
routes
=
[
{
path
:
'/'
,
// URL path
name
:
'home'
,
// Route name (optional, used in programmatic navigation)
component
:
HomeView
// The corresponding page component
}
,
{
// Dynamic route: :id is a placeholder matching any value
// /post/1, /post/2, /post/hello all match this route
path
:
'/post/:id'
,
name
:
'post'
,
// Lazy loading: loaded only when visited; the build splits it into a separate JS file
component
:
(
)
=>
import
(
'../views/PostView.vue'
)
}
]
// Create the router instance
const
router
=
createRouter
(
{
history
:
createWebHistory
(
)
,
// Use HTML5 History mode (URL looks like a normal path)
routes
// Register the route table
}
)
export
default
router
The difference between the two routing modes:createWebHistory produces clean URLs (/post/1) but requires server configuration support. If the deployment environment is uncertain, you can use createWebHashHistory, which has a # in the URL (/#/post/1) and needs no server configuration.
Register the Router in main.js
Example
RouterView is the core outlet of the routing system.
When the URL is / , it renders HomeView; when the URL is /post/1 , it renders PostView.
RouterLink — Declarative Navigation
RouterLink is a navigation component provided by Vue Router, replacing the traditional tag.
It does not trigger a page refresh; it only switches the displayed component via JS.
Example
Home
Read article {{ article.id }}
useRoute — Reading Route Parameters
In the detail page PostView, you need to know the article ID in the URL to find the corresponding article.
useRoute is used to read the current route information.
Example
Article Detail Page
Current article ID: {{ route.params.id }}
route.params.idvalue is always a string. Even if the URL is/post/3, what you get is'3'rather than a number3. When you need a number, useNumber(route.params.id)orparseIntto convert.
Hands-On: The Complete Article Detail Page
Make BlogCard clickable to navigate, and the detail page displays the full article content based on the ID.
Step 1: Modify BlogCard to Support Click Navigation
Example
{{ category }}
{{ title }}
{{ summary }}
{{ date }}
Step 2: Create the PostView Detail Page
The detail page reads the ID from the URL, then finds and displays the corresponding article from the article data.
Example
Article does not exist
Article with ID {{ id }} not found
Back to Home
{{ article.category }}
{{ article.title }}
← Back to Home
v-htmlis used to render an HTML string, but note: do not use it to render user input, otherwise there is an XSS security risk. The article content here is written by the developer, so it is safe.
Chapter Summary
In this chapter you learned the core usage of Vue Router: route table configuration (path mapped to component), RouterLink navigation, RouterView outlet, dynamic route /post/:id, and useRoute for reading route parameters.
Now the blog has a real multi-page experience — browse the list on the home page, click to enter the detail page to read the full content.