vue動態(tài)設置路由權限的主要思路
之前看到網上有些動態(tài)設置路由的,但是跟目前的項目不是很匹配,就自己動手實現(xiàn)了一種。主要思路就是:
1.配置路由的時候綁定好id,可后端開發(fā)完成后,與后端同步id就行,這id唯一不變,根據(jù)此id可找到路由地址及icon。
const routerArr = [ { path: '', name: '', component: () => import( /* webpackChunkName: "strategiesMaintain" */ '@/components/Layout/Index'), meta: { requireAuth: true, id: 1, icon: 'iconzhanghuguanli', title: '路由1' }, children: [{ path: '/verificationLog', name: 'VerificationLog', component: () => import( /* webpackChunkName: "verificationLog" */ '@/views/auditManage/verificationLog'), meta: { requireAuth: true, id: 101, icon: 'icon-disanfangyanzhengrizhi', title: '路由11' } }, { path: '/systemLog', name: 'SystemLog', component: () => import( /* webpackChunkName: "systemLog" */ '@/views/auditManage/systemLog'), meta: { requireAuth: true, id: 102, icon: 'icon-xitongcaozuorizhi', title: '路由12' } }] } ]; export default routerArr;
2.設置本地路由與后端傳來的路由的聯(lián)系,主要是根據(jù)id綁定路由地址及iconClass
import routerModules from "@/router/modules"; import {http} from '@/utils/http' import store from '@/store'; import { Message } from 'element-ui' const formateResData = (val) =>{ // 格式化路由數(shù)據(jù) const obj = {}; const fn = (arr)=>{ for(let i = 0,item;item = arr[i++];){ obj[item['meta']['id']] = { path: item['path'], iconClass: item['meta']['icon'] }; if(item.children && item.children.length > 0){ fn(item.children); } } } fn(val); return obj; }; const MAPOBJ = formateResData(routerModules); const dealWithData = (navData) => { // 處理菜單數(shù)據(jù) let firstLink = ""; const navIdArr = []; const fn = (arr) => { for (let i = 0,item;item = arr[i++];) { item['iconClass'] = MAPOBJ[item.id].iconClass; item['linkAction'] = MAPOBJ[item.id].path; navIdArr.push(item.id); if (!firstLink && !item.subMenu) { // 設置默認跳轉 firstLink = item['linkAction']; } if (item.subMenu && item.subMenu.length > 0) { fn(item.subMenu); } } } fn(navData); return {navData,navIdArr,firstLink}; }; let navIds = []; const getNav = async (to={},from={},next=()=>{})=>{ // 獲取導航數(shù)據(jù) const {code,data} = await http("/menu/list", {}, "GET"); // 獲取菜單數(shù)據(jù) // const data = require("@/mock/api/menuData"); // 使用mock數(shù)據(jù) const {navData,navIdArr,firstLink} = dealWithData(data); store.commit('setNavData', navData); navIds = navIdArr; if(to.fullPath == '/index'){ // 從登錄過來 或者是回首頁 next(firstLink); }else { // 刷新 if(navIds.indexOf(to.meta.id) == -1){ // 后端沒有返回該菜單 Message.error('菜單不存在或者沒有權限'); return; } next(); } } export const setGuard = (to={},from={},next=()=>{}) =>{ // 設置權限 if(navIds.length === 0){ // 還沒有獲取菜單數(shù)據(jù) getNav(to,from,next); }else { // 獲取到菜單數(shù)據(jù) if(navIds.indexOf(to.meta.id) == -1){ // 后端沒有返回該菜單 Message.error('菜單不存在或者沒有權限'); return; } next(); } }
3.在mainjs中引入配置
router.beforeEach((to, from, next) => { let token = wlhStorage.get("authorization"); if (to.path == "/login") { storage.clear();// 清空緩存 next(); } else { if (to.meta.requireAuth && token) { // 登陸 setGuard(to,from,next); } else { // 沒有登錄 next("/login"); } } })
總結
到此這篇關于vue動態(tài)設置路由權限的文章就介紹到這了,更多相關vue動態(tài)設置路由權限內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue?cli3?項目中如何使用axios發(fā)送post請求
這篇文章主要介紹了vue?cli3?項目中如何使用axios發(fā)送post請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04解決vue create 創(chuàng)建項目只有兩個文件問題
這篇文章主要介紹了解決vue create 創(chuàng)建項目只有兩個文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Vue路由對象屬性 .meta $route.matched詳解
今天小編就為大家分享一篇Vue路由對象屬性 .meta $route.matched詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11