Routing System
In the previous chapter, our task management logic was already persisted via Pinia. Now we need to inject a soul into the application—Routing System.
In enterprise development, routing is not just page switching; it also bears **security (access control)anduser experience (dynamic title)** responsibilities.
Installation and Basic Configuration
First install Vue Router:
npm install vue-router
in src directory, create a router directory, and create router/index.js files.

We will define two pages: the Workbench (Dashboard) and the Login page.
Example
import
{
createRouter
,
createWebHistory
}
from
'vue-router'
const
routes
=
[
{
path
:
'/login'
,
name
:
'Login'
,
component
:
(
)
=>
import
(
'@/views/Login.vue'
)
,
meta
:
{
title
:
'Login - TaskHub'
,
requiresAuth
:
false
}
}
,
{
path
:
'/'
,
name
:
'Dashboard'
,
component
:
(
)
=>
import
(
'@/views/Dashboard.vue'
)
,
// route meta info: used to store custom data
meta
:
{
title
:
'My Workbench'
,
requiresAuth
:
true
,
breadcrumb
:
'Home'
}
}
]
const
router
=
createRouter
(
{
history
:
createWebHistory
(
)
,
routes
}
)
export
default
router
in src create under views folder and assign the pages.
src/App.vue: root container, only holdsand global animations.src/views/Dashboard.vue: the original TaskHub core functionality (task list, input box, etc.).src/views/Login.vue: the newly added login page.
The complete structure is as follows:

Refactor App.vue (turn it into a clean container)
Example
Move the original logic into Dashboard.vue
Move the code previously written in App.vue (importing Store, importing components, template layout) all cut into src/views/Dashboard.vue it.
Dashboard.vue file code
v-for="task in filteredTasks" :key="task.id" :task="task" @toggle="toggleTask" @remove="removeTask" />
Login Logic Implementation (Login.vue)
Use Tailwind v4 to quickly build a minimalist login page and simulate login behavior.
Example
Welcome back
Improve router/index.js 's interception logic
Make sure your route configuration has enabled guard mode:
Example
import
{
createRouter
,
createWebHistory
}
from
'vue-router'
const
routes
=
[
{
path
:
'/login'
,
name
:
'Login'
,
component
:
(
)
=>
import
(
'@/views/Login.vue'
)
,
meta
:
{
title
:
'Login - TaskHub'
,
requiresAuth
:
false
}
}
,
{
path
:
'/'
,
name
:
'Dashboard'
,
component
:
(
)
=>
import
(
'@/views/Dashboard.vue'
)
,
// route meta info: used to store custom data
meta
:
{
title
:
'My Workbench'
,
requiresAuth
:
true
,
breadcrumb
:
'Home'
}
}
]
const
router
=
createRouter
(
{
history
:
createWebHistory
(
)
,
routes
}
)
// src/router/index.js (core snippet)
router.
beforeEach
(
(
to
,
from
,
next
)
=>
{
const
isAuthenticated
=
localStorage.
getItem
(
'isLoggedIn'
)
===
'true'
// if going to home but not logged in -> go to login
if
(
to.
meta
.
requiresAuth
&&
!
isAuthenticated
)
{
next
(
'/login'
)
}
// if already logged in but still want to go to login -> back to home
else
if
(
to.
path
===
'/login'
&&
isAuthenticated
)
{
next
(
'/'
)
}
else
{
next
(
)
}
}
)
export
default
router
in main.js complete the final piece in
Make sure your main.js imports and mounts the router:
Example
import
{
createApp
}
from
'vue'
import
{
createPinia
}
from
'pinia'
import
router from
'./router'
// import the router
import
App from
'./App.vue'
import
'./style.css'
const
app
=
createApp
(
App
)
app.
use
(
createPinia
(
)
)
app.
use
(
router
)
// 2. register the router
app.
mount
(
'#app'
)
Final Effect
- Visit /
:如果你没登录,页面会闪现一下然后跳转到/login. - Visit /login
:点击按钮,本地存储isLoggedIn变为true, then instantly switch to the workbench. - Refresh the page: Pinia reads tasks from LocalStorage, the route guard checks the login status, and no data is lost.

After logging in, you can click to log out:

Knowledge Points Explanation
- Why
App.vuebecame empty?
In a Single Page Application (SPA),App.vueis the shell of the entire application. We emptied it so that different pages (Dashboard or Login) can be dynamically inserted into it based on URL changes. useRouter's hands-on:
inDashboard.vue, we usedrouter.push('/login')to implement the logout feature. Note:pushadds a new record to the browser history; clicking the browser back button can go back.- Tailwind v4's Layout Inheritance:
Because we addedApp.vue's global container addedbg-slate-50andmin-h-screen, all child pages (Dashboard, Login) will inherit this light gray background by default, ensuring visual consistency.