vue3動(dòng)態(tài)路由解決刷新頁(yè)面空白或跳轉(zhuǎn)404問(wèn)題
前言
開(kāi)發(fā)后臺(tái)管理系統(tǒng)時(shí),有時(shí)后端會(huì)根據(jù)權(quán)限返回不同的菜單列表,前端需要異步獲取數(shù)據(jù)然后實(shí)時(shí)生成路由配置。
在vue3項(xiàng)目中,我們使用pinia、vue-router實(shí)現(xiàn)動(dòng)態(tài)路由,關(guān)鍵步驟如下:
- 異步請(qǐng)求獲取路由接口數(shù)據(jù);
- pinia狀態(tài)管理保存路由信息;
- vue-router實(shí)現(xiàn)路由配置;
- 動(dòng)態(tài)添加路由。
1 異步請(qǐng)求獲取路由接口數(shù)據(jù)
// /src/api/route.js
export const getRouteList = () => {
// 模擬異步請(qǐng)求
return new Promise((resolve) => {
setTimeout(() => {
resolve([
{
name: "home",
path: "/home",
meta: { title: "首頁(yè)" },
},
{
name: "user",
path: "/user",
meta: { title: "用戶" },
},
])
}, 1000)
})
}
2 pinia狀態(tài)管理保存路由信息
// /src/store/route.js
import { defineStore } from "pinia"
export const useRouteStore = defineStore("route", {
state: () => ({
routeList: sessionStorage.getItem("routeList")
? JSON.parse(sessionStorage.getItem("routeList"))
: [],
isUpdate: false,
}),
actions: {
updateRouteList(routeList) {
this.routeList = routeList
sessionStorage.setItem("routeList", JSON.stringify(routeList))
this.isUpdate = true
},
},
getters: {},
})
3 vue-router實(shí)現(xiàn)路由配置
import { createRouter, createWebHashHistory } from "vue-router"
// 定義基本路由配置
const routes = [
{
path: "/:pathMatch(.*)",
meta: { title: "404" },
name: "404",
component: () => import("@/views/error/404.vue"),
},
{
path: "/login",
name: "login",
component: () => import("@/views/login/Login.vue"),
meta: { title: "登錄" },
},
{
path: "/",
name: "layout",
component: () => import("@/views/layout/Layout.vue"),
redirect: "/home",
children: [], // 初始時(shí)沒(méi)有子路由
},
]
export const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes,
})
// 路由守衛(wèi),用于處理路由跳轉(zhuǎn)前的邏輯
router.beforeEach(async (to, from, next) => {
// 判斷是否已登錄且沒(méi)有 token,未登錄時(shí)重定向到登錄頁(yè)
const token = localStorage.getItem("token")
if (to.path !== "/login" && !token) {
return next({ name: "login" })
}
next()
})
4 動(dòng)態(tài)添加路由
核心代碼
router.addRoute("layout", {
path: item.path,
name: item.name,
component: () => import(`@/views/${item.name}/index.vue`),
})
完整代碼
// /src/router/index.js
import { createRouter, createWebHashHistory } from "vue-router"
import { useRouteStore } from "@/store/route"
import { getRouteList } from "@/api/route"
// 定義基本路由配置
const routes = [
{
path: "/:pathMatch(.*)",
meta: { title: "404" },
name: "404",
component: () => import("@/views/error/404.vue"),
},
{
path: "/login",
name: "login",
component: () => import("@/views/login/Login.vue"),
meta: { title: "登錄" },
},
{
path: "/",
name: "layout",
component: () => import("@/views/layout/Layout.vue"),
redirect: "/home",
children: [], // 初始時(shí)沒(méi)有子路由
},
]
export const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes,
})
// 添加動(dòng)態(tài)路由
const addRoute = () => {
const routeStore = useRouteStore()
if (routeStore.isUpdate) {
routeStore.routeList.forEach((item) => {
if (!router.hasRoute(item.name)) {
router.addRoute("layout", {
path: item.path,
name: item.name,
component: () => import(`@/views/${item.name}/index.vue`),
})
}
})
routeStore.isUpdate = false
}
}
// 初始化路由
export const initRouter = async () => {
let routeList = sessionStorage.getItem("routeList")
? JSON.parse(sessionStorage.getItem("routeList"))
: await getRouteList()
const routeStore = useRouteStore()
routeStore.updateRouteList(routeList)
addRoute()
}
// 路由守衛(wèi),用于處理路由跳轉(zhuǎn)前的邏輯
router.beforeEach(async (to, from, next) => {
// 添加動(dòng)態(tài)路由
addRoute()
// 判斷是否已登錄且沒(méi)有 token,未登錄時(shí)重定向到登錄頁(yè)
const token = localStorage.getItem("token")
if (to.path !== "/login" && !token) {
return next({ name: "login" })
}
next()
})
注意:動(dòng)態(tài)添加路由后刷新頁(yè)面會(huì)跳轉(zhuǎn)404頁(yè)面,因?yàn)樵谶M(jìn)路由守衛(wèi)之前,程序已經(jīng)進(jìn)行了路由匹配,結(jié)果就是沒(méi)匹配到任何內(nèi)容。
解決方案:在router注冊(cè)之前調(diào)用initRouter函數(shù)初始化路由。
// main.js
import "./assets/css/main.css"
import { createApp } from "vue"
import { createPinia } from "pinia"
import App from "./App.vue"
import { initRouter, router } from "./router"
const app = createApp(App)
const call = async () => {
app.use(createPinia())
await initRouter()
app.use(router)
app.mount("#app")
}
call()
到此這篇關(guān)于vue3動(dòng)態(tài)路由解決刷新頁(yè)面空白或跳轉(zhuǎn)404問(wèn)題的文章就介紹到這了,更多相關(guān)vue3刷新頁(yè)面空白或404內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用HBuilder打包前端開(kāi)發(fā)webapp為apk的方法
下面小編就為大家?guī)?lái)一篇利用HBuilder打包前端開(kāi)發(fā)webapp為apk的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Vue應(yīng)用中使用xlsx庫(kù)實(shí)現(xiàn)Excel文件導(dǎo)出的詳細(xì)步驟
本文詳細(xì)介紹了如何在Vue應(yīng)用中使用xlsx庫(kù)來(lái)導(dǎo)出Excel文件,包括安裝xlsx庫(kù)、準(zhǔn)備數(shù)據(jù)、創(chuàng)建導(dǎo)出方法、觸發(fā)導(dǎo)出操作和自定義Excel文件等步驟,xlsx庫(kù)提供了強(qiáng)大的API和靈活的自定義選項(xiàng),使得處理Excel文件變得簡(jiǎn)單而高效2024-10-10
vue實(shí)現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問(wèn)題(推薦)
最近公司項(xiàng)目需要,利用vue實(shí)現(xiàn)pdf導(dǎo)出,從而保存到本地打印出來(lái),說(shuō)起來(lái)好像也很容易,具體要怎么實(shí)現(xiàn)呢?下面小編給大家?guī)?lái)了vue實(shí)現(xiàn)pdf導(dǎo)出解決生成canvas模糊等問(wèn)題,需要的朋友參考下吧2018-10-10
vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解
這篇文章主要介紹了vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue使用高德地圖選點(diǎn)定位搜索定位功能實(shí)現(xiàn)
這篇文章主要介紹了Vue使用高德地圖選點(diǎn)定位搜索定位功能,文中給大家提到了常見(jiàn)問(wèn)題解決方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Vue?echarts實(shí)例項(xiàng)目商家銷(xiāo)量統(tǒng)計(jì)圖實(shí)現(xiàn)詳解
Echarts,它是一個(gè)與框架無(wú)關(guān)的?JS?圖表庫(kù),但是它基于Js,這樣很多框架都能使用它,例如Vue,估計(jì)IONIC也能用,因?yàn)槲业牧?xí)慣,每次新嘗試做一個(gè)功能的時(shí)候,總要新創(chuàng)建個(gè)小項(xiàng)目,做做Demo2022-09-09
Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套
本篇文章主要介紹了Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
vue實(shí)現(xiàn)三級(jí)導(dǎo)航展示和隱藏
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)三級(jí)導(dǎo)航展示和隱藏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

