Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Routing System

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.

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

Login Logic Implementation (Login.vue)

Use Tailwind v4 to quickly build a minimalist login page and simulate login behavior.

Example


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

  1. Visit /:如果你没登录,页面会闪现一下然后跳转到/login.
  2. Visit /login:点击按钮,本地存储isLoggedIn变为true, then instantly switch to the workbench.
  3. 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

  1. Why App.vue became empty?
    In a Single Page Application (SPA),App.vue is 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.
  2. useRouter 's hands-on:
    in Dashboard.vue , we used router.push('/login') to implement the logout feature. Note:push adds a new record to the browser history; clicking the browser back button can go back.
  3. Tailwind v4's Layout Inheritance:
    Because we added App.vue 's global container added bg-slate-50 and min-h-screen, all child pages (Dashboard, Login) will inherit this light gray background by default, ensuring visual consistency.