Vue3實現(xiàn)動態(tài)路由與手動導(dǎo)航
在后臺管理系統(tǒng)中,前端的路由往往需要根據(jù)用戶的權(quán)限動態(tài)生成。這篇文章將重點介紹如何在 Vue 3 中實現(xiàn)動態(tài)路由注冊和手動導(dǎo)航,確保用戶訪問的頁面與權(quán)限對應(yīng)。
1. 動態(tài)路由的需求與原理
為什么需要動態(tài)路由?
- 權(quán)限控制:不同用戶角色需要看到不同的菜單和頁面。
- 后端驅(qū)動:后端返回菜單數(shù)據(jù),前端動態(tài)渲染菜單和注冊路由。
- 避免硬編碼:路由不再寫死在前端代碼里,保證靈活性。
動態(tài)路由的原理
- 靜態(tài)路由:定義公共頁面,如登錄頁和404頁面。
- 動態(tài)路由:存儲需要通過后端數(shù)據(jù)動態(tài)添加的頁面。
- 動態(tài)注冊路由:根據(jù)后端返回的數(shù)據(jù),通過
router.addRoute動態(tài)添加到路由系統(tǒng)。 - 手動導(dǎo)航:添加新路由后,需要手動觸發(fā)導(dǎo)航以保證路由生效。
2. 靜態(tài)路由與動態(tài)路由配置
靜態(tài)路由
靜態(tài)路由是所有用戶共享的基本路由,如登錄頁、404頁等。
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import Layout from '@/layout/admin.vue';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
component: Layout,
name: 'admin',
},
{
path: '/login',
name: 'login',
component: () => import('@/pages/login/index.vue'),
meta: { title: '登錄頁' },
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => import('@/pages/404/index.vue'),
meta: { title: '404' },
},
];
動態(tài)路由
動態(tài)路由需要根據(jù)后端返回的數(shù)據(jù)進行注冊:
const asyncRoutes: Array<RouteRecordRaw> = [
{
path: '/',
name: '/',
component: () => import('@/pages/index/index.vue'),
meta: { title: '首頁' },
},
{
path: '/goods/list',
name: '/goods/list',
component: () => import('@/pages/goods/list.vue'),
meta: { title: '商品列表' },
},
{
path: '/category/list',
name: '/category/list',
component: () => import('@/pages/category/list.vue'),
meta: { title: '分類列表' },
},
];3. addRoutes 實現(xiàn)動態(tài)路由注冊
addRoutes 方法
該方法接收后端返回的菜單數(shù)組,并將匹配的動態(tài)路由添加到主路由 admin 下。
import { router } from '@/router/index';
export const addRoutes = (routesArray: Array<MenusArray>) => {
let hasNewRoutes = false;
const addRoutesFromMenus = (menus: Array<MenusArray>) => {
menus.forEach((menu) => {
// 查找匹配的動態(tài)路由
const route = asyncRoutes.find((r) => r.path === menu.frontpath);
// 添加到router中
if (route && !router.hasRoute(route.path)) {
router.addRoute('admin', route);
hasNewRoutes = true;
}
// 遞歸處理子菜單
if (menu.child && menu.child.length > 0) {
addRoutesFromMenus(menu.child);
}
});
};
addRoutesFromMenus(routesArray);
return hasNewRoutes;
};解釋
router.addRoute:Vue Router 提供的 API,可以動態(tài)添加路由。- 避免重復(fù)添加:
router.hasRoute檢查路由是否已存在,防止重復(fù)注冊。 - 遞歸處理:支持多級菜單,遞歸遍歷
child子菜單。
4. 路由守衛(wèi)中調(diào)用 addRoutes
在 router.beforeEach 路由守衛(wèi)中,調(diào)用 addRoutes 注冊動態(tài)路由,并實現(xiàn)手動導(dǎo)航。
import { getToken } from '@/utils/auth';
import store from '@/store';
import { addRoutes } from '@/router/index';
router.beforeEach(async (to, from, next) => {
const token = getToken();
let hasNewRoutes = false;
// 顯示全局加載狀態(tài)
showFullLoading();
// 未登錄跳轉(zhuǎn)到登錄頁
if (!token && to.name !== 'login' && to.name !== 'NotFound') {
return next('/login');
}
// 已登錄訪問登錄頁,重定向到主頁
if (token && to.name === 'login') {
return next('/');
}
// 已登錄狀態(tài)下,動態(tài)添加路由
if (token) {
await store.dispatch('user/getUserInfo'); // 拉取用戶信息
hasNewRoutes = addRoutes(store.getters['menu/getMenus']); // 添加動態(tài)路由
}
// 設(shè)置頁面標題
if (to.meta.title) {
document.title = `${to.meta.title}-測試后臺管理`;
} else {
document.title = '測試后臺管理';
}
// 手動導(dǎo)航:如果添加了新路由,重新跳轉(zhuǎn)當(dāng)前頁面
hasNewRoutes ? next(to.fullPath) : next();
});手動導(dǎo)航的必要性
router.addRoute是動態(tài)操作,添加新路由后需要重新跳轉(zhuǎn)一次,確保用戶能正常訪問新注冊的頁面。next(to.fullPath):手動跳轉(zhuǎn)到當(dāng)前頁面。
5. 完整流程
- 用戶登錄:獲取用戶
token,拉取用戶信息。 - 獲取菜單數(shù)據(jù):后端返回用戶權(quán)限對應(yīng)的菜單數(shù)據(jù)。
- 動態(tài)注冊路由:調(diào)用
addRoutes將菜單數(shù)據(jù)匹配的路由添加到admin下。 - 手動導(dǎo)航:動態(tài)路由添加完成后,使用
next(to.fullPath)手動觸發(fā)頁面跳轉(zhuǎn)。 - 權(quán)限生效:用戶只能訪問與自己權(quán)限匹配的頁面。
6. 總結(jié)
- 靜態(tài)路由:用于公共頁面。
- 動態(tài)路由:后端驅(qū)動,通過
addRoutes動態(tài)注冊。 - 手動導(dǎo)航:解決動態(tài)添加路由后無法直接訪問的問題。
- 靈活性:動態(tài)路由使前端代碼更靈活,后端控制權(quán)限更方便。
以上就是Vue3實現(xiàn)動態(tài)路由與手動導(dǎo)航的詳細內(nèi)容,更多關(guān)于Vue3動態(tài)路由與手動導(dǎo)航的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3中使用reactive定義的變量響應(yīng)式丟失問題解決方案
這篇文章主要介紹了vue3中使用reactive定義的變量響應(yīng)式丟失問題的具體例子和解決方案,文章通過代碼示例給大家講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06

